Python module search path issue

I'm trying to work in a dev environment, but I find problems in that python seems to be using modules from the site-packages directory. I want it to use modules from my dev directory.

sys.path returns a bunch of dirs like

['', '/usr/lib/python26.zip', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/site-packages' etc

That's good, it uses the current directory as the first search location (at least as I understand it).

Ok now, if I create a file called command.py in the current directory, everything works as I would expect.

>>> import commands
>>> commands.__file__
   'commands.pyc'

Then I exit the python shell and run another. Then I do it.

>>> import foo.bar.commands

Now, what do I expect from this, I need to go down from the current directory to. / Foo / bar / and get the command module from me. I understand that this

>>> foo.bar.commands.__file__
    '/usr/lib/python2.6/site-packages/foo/bar/commands.pyc'

Despite the fact that there is from my current directory. /foo/bar/commands.py

imp.find_module() imp.load_module() . ( , ), ,

>>> import foo.bar.commands
>>> foo.bar.commands.__file__
   '/usr/lib/python2.6/site-packages/foo/bar/commands.pyc'
>>> foo.bar.__file__
   '/usr/lib/python2.6/site-packages/foo/bar/__int__.pyc'
>>> foo.__file__
    './foo/__init__.pyc'

, foo/ init.pyc , ?

+3
2

, foo, , foo/__init__.py ( ): , Python, foo . foo/bar/__init__.py - , foo/__init__.py , foo.bar .

, .pth / __path__ , , __init__.py , Python . "" , , import foo foo - foo/__init__.py, , ( , , - ).

, , - ( sys.path) . python -v , . -

import pdb; pdb.set_trace()

, , , , sys.path, sys.modules(, , , ) , sys.modules ['foo'], ? imp, , .

+3

foo /usr/lib/python 2.6/site-packages? , foo , , .

foo/bar

, :

/foo/__init__.py  
    /bar/__init__.py
        /commands.py

, python - .py - ?

0

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


All Articles