To import a module from another directory in Python, you can use the sys.path.append
function to add the directory to the Python path, and then use the import
statement to import the module.
For example, suppose you have the following directory structure:
Code language: Python (python)project/ app/ __init__.py main.py module1/ __init__.py foo.py module2/ __init__.py bar.py
And you want to import the foo
module from the module1
directory in the app/main.py
file. You can do this by adding the project
directory to the Python path and then importing the module1.foo
module:
import sys
sys.path.append("/path/to/project")
import module1.foo
Code language: Python (python)
Alternatively, you can use the PYTHONPATH
environment variable to specify directories that should be added to the Python path. For example:
export PYTHONPATH="/path/to/project:$PYTHONPATH"
Code language: Python (python)
Then you can import the bar
module in the main.py
file as follows:
import module1.foo
Code language: Python (python)
Note that the PYTHONPATH
environment variable is a list of directories separated by colons, and it is used by Python to search for modules when you use the import
statement.
Also note that we added the /path/to/project
directory to the path and not /path/to/project/module1
. This is so that we can also import any other modules inside the project
directory.
For example, you can now also import module2.bar
:
import module2.bar
Code language: Python (python)
If we added the project/module1
directory to the path instead then we would also have to add each new module individually to the path before importing them. But this way, we can add new modules as needed without changing the PYTHONPATH
again.
Suppose you have a function called my_func
defined in module1/foo.py
. Here is how you could call it from app/main.py
or module2/bar.py
:
import module1.foo
module1.foo.my_func()
Code language: Python (python)
Alternatively, you can import my_func
directly:
from module1.foo import my_func
my_func() # this will also work
Code language: Python (python)
Relative Imports
You might have seen other answers online recommending to use relative imports.
For example, you might have seen somebody recommend doing this from app/main.py
:
from ..module1 import foo
Code language: Python (python)
However, relative imports will not work unless your code is being run as a module. If you are simply running it from the command like python3 app/main.py
then you cannot use relative imports and will receive an error like this:
Code language: plaintext (plaintext)ImportError: attempted relative import with no known parent package
In an attempt to fix this, you might run python3 -m app1.main
but then you will likely receive a different error:
Code language: plaintext (plaintext)ValueError: attempted relative import beyond top-level package
To workaround these issues I recommend avoiding relative imports and instead modifying the PYTHONPATH
environment variable as described above for the most effective quick fix.
Alternatively, if you want to package your code so that you can use relative imports and avoid modifying the path then you can follow the Packaging Python Projects guide.
Leave a Reply