Python gains access to modules from a package that is distributed across various directories

I have a question regarding a single module that is distributed across several directories.

Let's say I have these two files and directories:

~/lib/python
   xxx
      __init__.py
      util
         __init__.py
         module1.py
         module2.py
~/graphics/python
   xxx
      __init__.py
      misc
         __init__.py
         module3.py
         module4.py

So then in my Python modules I did this:

import sys
pythonlibpath = '~/lib/python'
if pythonlibpath not in sys.path: sys.path.append(pythonlibpath)
import xxx.util.module1

which is working.

Now the problem is that I need xxx.misc.module3, so I did this:

import sys
graphicslibpath = '~/graphics/python'
if graphicslibpath not in sys.path: sys.path.append(graphicslibpath)
import xxx.misc.module3

but I get this error:

ImportError: No module named misc.module3

He seems to somehow remember that there was an xxx package in ~ / lib / python, and then he tries to find misc.module3 there.

How do I get around this?

+3
source share
4 answers

, . Python , . . os, , os.path.

+2

Python , xxx. , . , , sys.modules.

sys.modules , , , :

import sys
print sys.modules
import xml
print sys.modules
del sys.modules['xml']
print sys.modules

, xml , . , , . , misc util , . , , Python.

+1

Python 3.3. . PEP-420.

+1

.

In response to @Gary's answer, the PEP 420 page says to use the following code in shared packages __init__.py.

__init__.py:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

This code must be placed in the xxx directory __init__.py. See * S below

someroot/
β”œβ”€β”€ graphics
β”‚   └── python
β”‚       └── xxx
β”‚           β”œβ”€β”€ ****__init__.py****
β”‚           └── misc
β”‚               β”œβ”€β”€ __init__.py
β”‚               β”œβ”€β”€ module3.py
β”‚               └── module4.py
└── lib
    └── python
        └── xxx
            β”œβ”€β”€ ****__init__.py****
            └── util
                β”œβ”€β”€ __init__.py
                β”œβ”€β”€ module1.py
                └── module2.py

Some setup.shfile to add in Python Path:

libPath=someroot/lib/python/
graphicsPath=someroot/graphics/python/
export PYTHONPATH=$PYTHONPATH:$libPath:$graphicsPath

Python test code (tested on versions 2.4.14 and 3.6.4 in Python using pyenv ):

import xxx.util.module1
import xxx.misc.module3 # No errors
0
source

Source: https://habr.com/ru/post/1745740/


All Articles