Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: name of parent package not recognized in import statements

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 the sound.effects package, it can use from sound.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
              ...
like image 713
Victor Odouard Avatar asked Sep 08 '17 07:09

Victor Odouard


People also ask

How do I import a parent folder?

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.

Why is Python not importing module?

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.

Why do I need __ init __ py?

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.


1 Answers

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 :

  1. Use the notation : from .binconvert import tocsv (conform to PEP 328)
  2. Move up from 1 directory before start and launch process.py from the Mapper directory
  3. Change the PYTHONPATH environment variable before launching process.py by adding the Mapperpath
like image 138
Cédric Julien Avatar answered Oct 24 '22 17:10

Cédric Julien