I have a very simple scenario like this:
example/
common.py
folder1/
script.py
where script.py
file should import common.py
module.
If I cd
into folder1/
and run the script (i.e. by calling python3 script.py
on the command line), the import breaks with the usual error ModuleNotFoundError: No module named 'common'
.
I know I could turn the whole parent folder into a package by adding __init__.py
files in each subdirectory, however this solution still prevents me to run the script directly from inside folder1/
.
How can I fix this?
If you turn both directories into python packages, and the top directory is in your PYTHONPATH, you can run script.py like so:
python3 -m example.folder1.script
If you need to add a parent directory to the search path, you can always do:
import os
import sys
DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.insert(0, DIR)
os.path.realpath(__file__)
gets the realpath to the file, and os.path.dirname
gets the directory name of this file. Doing it twice gets the example
directory here. Now, since this is added to the search path, you can import common
.
However, you should really consider having a dedicated scripts
or bin
directory, rather than try to run scripts from subdirectories. Think about writing a library, and import this library rather than individual files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With