Madi Poupou
Madi Poupou

Reputation: 23

How to import the right way in python?

I am learning python programming and I would like to have some information.
I am working on a small project, using OOP. Here is an example of the project structure:

PROJECT_FOLDER  
    |__ main.py  
    |__ modules  
         |__ __init__.py  
         |__ Example.py  

To import my classes (here just one, but generally more) should I import every file one by one or create an "all" rule in the __init__.py file?

Here are my ideas:

# Example.py
class Example:
    def __init__(self):
        print('Hello from Example')

First Idea:

# First version of __init__.py is an empty file
# First version of main.py
from modules.Example import Example

my_example = Example()

Or, second idea:

# Second version of __init__.py
__all__ = ['Example']
# Second version of main.py
from modules import *

my_example = Example.Example()

According to what I read in the documentation and here, it's better avoiding the use of import * because it could be confusing. But, if I need to import 56 classes, should I write 56 lines to import my classes before starting coding? And, using import * would force me to write my_example = Example.Example() which is not pretty and could be confusing too.

To conclude, is this better to name class files with the class names (including the capitalized first letter)? I learned that it was a good practice in PHP but python seems to have other convention (snake case for example) that pylint is always happy to remind me :)

Thanks for your answers, have a good day!

Upvotes: 2

Views: 517

Answers (2)

Eike Rabetian
Eike Rabetian

Reputation: 86

There are some general conventions when it comes to importing in python. import * is called wild card import and it's best to avoid using wild card imports no matter which programming language you're using. if all of your 56 classes are in one file, you import it like from my_file import class_1, class_2, .... you can use parentheses to make it more cleaner, from my_file import (class_1, class_2, class_3, ...) if your classes aren't in one file import them in init.py and then you can import all of them in one line like the above example . Another convention to importing in python is to always start from importing standard libraries and then third party packages and finally your own modules. for example

import sys

import requests

from my_package import some_thing

class in python should be CamelCase and for modules separate words with underscore. from MyClassName import my_module

Upvotes: 3

AKX
AKX

Reputation: 169051

According to what I read in the documentation and here, it's better avoiding the use of import * because it could be confusing.

This is true. Avoid import * if you can.

But, if I need to import 56 classes, should I write 56 lines to import my classes before starting coding?

Yes. But how many files do you foresee using 56 classes?

And, using import * would force me to write my_example = Example.Example() which is not pretty and could be confusing too.

I think you mean not using import *, but there's also the from module import names syntax, so you can do from modules.example import Example, and use my_example = Example().

To conclude, is this better to name class files with the class names (including the capitalized first letter)? I learned that it was a good practice in PHP but python seems to have other convention (snake case for example) that pylint is always happy to remind me :)

Conventionally, modules are snake_case.py, classes are PascalCase, names of variables and functions are snake_case, and constants are BIG_SNAKE_CASE.

Upvotes: 1

Related Questions