I know of one or more reported cases where python imports failed.
Because python packages are arranged with reference to the file system structure, running a module that imports a package that is one-level up in the file hierarchy results in an ImportError
.
This error can be resolved in any of the following ways:
-
Export
$PYTHONPATH
variable in your terminalexport PYTHONPATH=.
Check out this Enthought article about other alternatives.
-
Programmatically append the module's base path to
sys.path
import sys sys.path.append('./../')
-
Use Path entry finders ✔️
While rambling about trying to run a module that imports a name from another module, I came upon a documentation about Path entry finders in the packages section of python docs
Simply set
__path__
to a list or tuple of paths where python can lookup modules.For my example package in this repository, I set
__path__ = ['./../']
in the module I that intend to run.python3 one/b.py two
See an example package on using Path entry finders
Thanks. This helps.
Just to add to what you have from what I found on stackoverflow, If you just want to run the file, you could run it with the
-m
flag.or you can add this at the top of the file
or just add this
This also has some useful tip, I have not tried it yet though, using a main.py file at the root of the project. https://docs.python.org/3/library/__main__.html