Python模块搜索路径问题 [英] Python module search path problem

查看:99
本文介绍了Python模块搜索路径问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在开发环境上工作,但发现问题在于python似乎正在使用site-packages目录中的模块.我希望它使用我的dev目录中的模块.

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

sys.path这样返回一堆dirs

sys.path returns a bunch of dirs, like this

['', '/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

这很好,它使用当前目录作为查找的第一位(至少这是我的理解).

This is good, it's using the current directory as the first place of lookup (at least this is how I understand it to be).

好吧,如果我在当前目录中创建一个名为command.py的文件,一切都会按预期进行.

Ok now if I create say a file called command.py in the current directory, things work as I would expect them.

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

然后我退出python shell,并启动另一个.然后,我这样做.

I then exit out of the python shell, and start another one. I then do this.

>>> import foo.bar.commands

现在,我期望它执行的操作是从当前目录下移至./foo/bar/并从此处获取命令模块.我得到的是这个

Now, what I'm expecting it to do is go down from the current directory to ./foo/bar/ and get me the commands module from there. What I get though is this

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

即使在当前目录中,也有一个./foo/bar/commands.py

Even though from my current directory there is a ./foo/bar/commands.py

使用imp.find_module()和imp.load_module()可以正确加载本地模块.真正有趣的是(尽管我真的不知道这是什么意思)是按此顺序打印的最后一行

Using imp.find_module() and imp.load_module() I can load the local module properly. Whats actually interesting (although I don't really know what it means) is the last line that is printed out in this sequence

>>> 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,为什么不能在本地目录中找到其他文件?

So if it can find the foo/init.pyc in the local dir why can't it find the other files in the local dir?

欢呼

推荐答案

您提到在当前目录下有一个foo目录,但是您没有告诉我们foo/__init__.py是否存在(甚至可能为空):如果并非如此,这告诉Python foo不是不是软件包.对于foo/bar/__init__.py同样-如果该文件不存在,即使foo/__init__.py确实存在,则foo.bar也不是程序包.

You mention that there's a foo directory under your current directory, but you don't tell us whether foo/__init__.py exists (even possibly empty): if it doesn't, this tells Python that foo is not a package. Similarly for foo/bar/__init__.py -- if that file doesn't exist, even if foo/__init__.py does, then foo.bar is not a package.

您可以通过在软件包中放置.pth文件和/或显式设置__path__来进行一些操作,但是基本的简单规则是将__init__.py放置在您希望Python识别的每个目录中作为包装.该文件的内容是包本身的主体",因此,如果import foofoo是包含foo/__init__.py文件的目录,则就是要导入的内容(无论如何,包的主体第一次从包或其子包中导入任何内容时执行.

You can play around a little by placing .pth files and/or setting __path__ explicitly in your packages, but the basic, simple rule is to just place an __init__.py in every directory that you want Python to recognize as a package. The contents of that file are "the body" of the package itself, so if you import foo and foo is a directory with a foo/__init__.py file, then that's what you're importing (in any case, the package's body executes the first time you import anything from the package or any subpackage thereof).

如果这不是问题,则可能是其他导入(或显式的sys.path操作)可能使您感到困惑.使用-v标志运行python可使导入高度可见,这可以有所帮助.另一个好的技术是放置一个

If that is not the problem, it looks like some other import (or explicit sys.path manipulation) may be messing you up. Running python with a -v flag makes imports highly visible, which can help. Another good technique is to place an

import pdb; pdb.set_trace()

就在您认为导入行为不正确之前,并在此时检查sys.path,sys.modules(以及可能的其他高级结构,例如import钩子)之前,是否已经定义了sys.modules ['foo'] , 例如?交互尝试使用标准库模块imp中代表您给定路径定位模块的功能也可能具有启发性.

just before the import that you think is misbehaving, and examining sys.path, sys.modules (and possibly other advanced structures such as import hooks) at that point - is there a sys.modules['foo'] already defined, for example? Interactively trying the functions from standard library module imp that locate modules on your behalf given a path may also prove instructive.

这篇关于Python模块搜索路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆