I understand that there are many questions on this topic, but most of the answers I've seen describe complex workarounds to problems that should, it seems to me, be simple. Here is my directory structure:
Mapper/
LICENSE.txt
README.txt
setup.py
bin/
# nothing here, yet
mapper/
__init__.py
process.py
where.py
# ...
binconvert/
__init__.py
tocsv.py
todict.py
# ...
I would like to use absolute paths on all of my locally developed modules to decrease confusion and to avoid the bugs mentioned here.
However, I am having multiple issues with this. When I run process.py
, in which I import
tocsv.py
like so,
from mapper.binconvert import tocsv
I get the error: ModuleNotFoundError: No module named 'mapper'
I know I could have done from binconvert import tocsv
, but like I said I am trying to have all my locally developed module imports use absolute paths.
I get the same error as before in tocsv.py
when I am trying to import where.py
like so:
from mapper import where
For this one I believe there are ways to use the dot syntax relative import
, but again, I would like to keep all my paths absolute. After all, I believe that according to this passage of python documentation, I should be able to do this:
When packages are structured into subpackages (as with the sound package in the example), you can use absolute imports to refer to submodules of siblings packages. For example, if the module
sound.filters.vocoder
needs to use the echo module in thesound.effects package
, it can use fromsound.effects import echo
.
sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
aiffread.py
aiffwrite.py
auread.py
auwrite.py
...
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
...
filters/ Subpackage for filters
__init__.py
equalizer.py
vocoder.py
karaoke.py
...
Method 1: Import from parent directory using sys.Add the parent directory to the sys. path using the append() method. It is a built-in function of the sys module that can be used with a path variable to add a specific path for interpreters to search. The following example shows how this can be done.
This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.
The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.
When you execute process.py
you're already located inside the mapper package. Python will look through all the path defined in sys.path
to find module, which in this case is only made of ["standard python path", "Mapper/mapper"]
.
In this case, python won't find a module named mapper inside those directories (you already are IN the mapper module).
Solutions for you :
from .binconvert import tocsv
(conform to PEP 328)process.py
from the Mapper
directoryprocess.py
by adding the Mapper
pathIf 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