What is the correct way to handle import in a complex Python project?

I always used Python for single-file scripts, and now I'm working on my first “complex” project, so I have no experience with packages in Python (3.x).

I know this is a frequently asked question, but I am missing something to successfully organize the import of modules in my project, which should be used as a library.

Assuming the following project tree:

myProject/
- subpackage1/
   - __init__.py
   - other1.py
- subpackage2/
   - __init__.py
   - other2.py
- __init__.py
- foo.py
- bar.py

What is the correct way to import modules from each other? In particular:

  • import foofrombar
  • import foofromother1
  • import other1fromother2
  • import foo(which is a library) from another directory at design time (are virtual environments the right choice?)

Thank!

EDIT: ( ), , , , . , , , from myProject.subpackage1.other1 import something. ( , Java), , , - . virtualenv , ?

+4
1

1. foo bar

, , from .. :

from . import foo

bar python bar.py, , :

import foo

, (sys.path/PYTHONPATH). , .

2. foo other1

, . :

from .. import foo

3. other1 other2

, :

from ..subpackage1 import other1

4. foo ()

, " " " ". , , foo distutils/setuptools, virtualenv .

, , , , setuptools .

+2

All Articles