Python is installed in a local directory.
Python安装在本地目录中。
My directory tree looks like this:
我的目录树是这样的:
(local directory)/site-packages/toolkit/interface.py
My code is in here:
我的代码在这里:
(local directory)/site-packages/toolkit/examples/mountain.py
To run the example, I write python mountain.py
, and in the code I have:
为了运行这个示例,我编写了python mountain。py,在我的代码中:
from toolkit.interface import interface
And I get the error:
我得到了误差:
Traceback (most recent call last):
File "mountain.py", line 28, in ?
from toolkit.interface import interface
ImportError: No module named toolkit.interface
I have already checked sys.path
and there I have the directory /site-packages
. Also, I have the file __init__.py.bin
in the toolkit folder to indicate to Python that this is a package. I also have a __init__.py.bin
in the examples directory.
我已经查过系统了。路径和我有目录/站点包。另外,我还有文件__init__.py。工具箱文件夹中的bin用于向Python表明这是一个包。我也有一个。在示例目录中的bin。
I do not know why Python cannot find the file when it is in sys.path
. Any ideas? Can it be a permissions problem? Do I need some execution permission?
我不知道为什么Python在sys.path中找不到这个文件。什么好主意吗?可能是权限问题吗?我需要执行许可吗?
20 个解决方案
#1
206
Based on your comments to orip's post, I guess this is what happened:
根据你对奥里普的评论,我猜是这样的:
- You edited
__init__.py
on windows. - 你编辑__init__。py在windows。
- The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
- windows编辑器添加了一些非打印的东西,可能是一个传输返回(在windows中,行尾是CR/LF;在unix中,它仅仅是LF,或者可能是CTRL-Z (windows文件结束)。
- You used WinSCP to copy the file to your unix box.
- 您使用WinSCP将文件复制到您的unix框中。
- WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
- WinSCP认为:“这不是一个基本的文本;我用。bin扩展来表示二进制数据
- The missing
__init__.py
(now called__init__.py.bin
) means python doesn't understand toolkit as a package. - 失踪的__init__。py(现在称为__init__.py.bin)意味着python不理解工具包作为一个包。
- You create
__init__.py
in the appropriate directory and everything works... ? - 您创建__init__。py在适当的目录下,一切正常……吗?
#2
54
Does
做
(local directory)/site-packages/toolkit
have a __init__.py
?
有行吗?
To make import walk through your directories every directory must have a __init__.py
file.
要使导入遍历您的目录,每个目录必须有一个__init__。py文件。
#3
33
On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:
在*nix上,还要确保正确配置了PYTHONPATH,特别是它具有以下格式:
.:/usr/local/lib/python
(Mind the .:
at the beginning, so that it can search on the current directory, too.)
(注意:在开头,这样它也可以在当前目录上搜索。)
It may also be in other locations, depending on the version:
它也可能在其他地点,取决于版本:
.:/usr/lib/python
.:/usr/lib/python2.6
.:/usr/lib/python2.7 and etc.
#4
24
I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:
我在LPTHW做这个练习的时候遇到了一些非常相似的东西;我无法让Python识别我正在调用的目录中有文件。但我最终还是成功了。我所做的,也是我推荐的,就是尝试一下:
(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)
(注意:从您最初的文章中,我假设您使用的是基于* nix的机器,并且正在从命令行运行东西,因此本文的建议是针对此进行调整的。既然我运行Ubuntu,这就是我所做的)
1) Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the mountain.py
file, and trying to call the toolkit.interface.py
module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is the toolkit
directory.
1)将目录(cd)更改为文件所在目录上方的目录。在这种情况下,你是在试图跑山。py文件,并尝试调用toolkit.interface.py模块,它位于不同的目录中。在本例中,您将访问包含这两个文件的路径的目录(或者换句话说,这两个文件的路径共享的最近的目录)。这里是工具箱目录。
2) When you are in the tookit
directory, enter this line of code on your command line:
2)在tookit目录下,在命令行上输入这一行代码:
export PYTHONPATH=.
出口到PYTHONPATH =。
This sets your PYTHONPATH to ".", which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn't just look in your current directory, but in all the directories that are in your current directory).
这将把python路径设置为“。”,这基本上意味着您的PYTHONPATH现在将在您当前所在的目录中查找任何被调用的文件(更重要的是,在您所在目录的子目录分支中)。因此,它不仅在当前目录中查找,而且在当前目录中的所有目录中查找。
3) After you've set your PYTHONPATH in the step above, run your module from your current directory (the toolkit
directory). Python should now find and load the modules you specified.
3)在上面步骤中设置好python路径之后,从当前目录(工具包目录)运行模块。Python现在应该找到并加载您指定的模块。
Hope this helps. I was quite frustrated with this myself.
希望这个有帮助。我自己也很沮丧。
#5
21
I solved my own problem, and I will write a summary of the things that were wrong and the solution:
我解决了我自己的问题,我将写一份关于错误和解决方案的摘要:
The file needs to be called exactly __init__.py
. If the extension is different such as in my case .py.bin
then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.
文件需要被准确地称为__init__.py。如果扩展是不同的,比如我的。py。bin那么Python就不能在目录中移动,然后它就找不到模块。要编辑文件,您需要使用Linux编辑器,如vi或nano。如果你使用Windows编辑器,这将会写一些隐藏的字符。
Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do which python
, and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.
另一个影响它的问题是,我已经安装了另一个Python版本的根,所以如果某人正在使用Python的本地安装,请确保运行该程序的Python安装是本地Python。要检查这个,只需执行哪个python,并查看可执行文件是否位于本地目录中。如果没有,请更改路径,但是要确保本地Python目录比其他Python更早。
#6
13
To mark a directory as a package you need a file named __init__.py
, does this help?
要将一个目录标记为一个包,您需要一个名为__init__的文件。py,这有帮助吗?
#7
6
- You must have the file __ init__.py in the same directory where it's the file that you are importing.
- 你必须把文件保密。py在同一个目录中,它是您正在导入的文件。
- You can not try to import a file that has the same name and be a file from 2 folders configured on the PYTHONPATH.
- 您不能尝试导入具有相同名称的文件,并从在PYTHONPATH上配置的两个文件夹中作为一个文件。
eg: /etc/environment
例如:/etc/environment
PYTHONPATH=$PYTHONPATH:/opt/folder1:/opt/folder2
PYTHONPATH = $ PYTHONPATH:/ opt / folder1 / opt / folder2
/opt/folder1/foo
/ opt / folder1 / foo
/opt/folder2/foo
/ opt / folder2 / foo
And, if you are trying to import foo file, python will not know which one you want.
如果您正在尝试导入foo文件,python将不知道您想要哪个文件。
from foo import ... >>> importerror: no module named foo
从foo进口…没有命名为foo的模块
#8
6
Using PyCharm
(part of the JetBrains suite) you need to define your script directory as Source:Right Click > Mark Directory as > Sources Root
使用PyCharm (JetBrains套件的一部分),您需要定义您的脚本目录作为源:右击>标记目录作为>源根。
#9
6
an easy solution is to install the module using python -m pip install <library-name>
instead of pip install <library-name>
you may use sudo in case of admin restrictions
一个简单的解决方案是使用python -m pip安装
#10
5
Yup. You need the directory to contain the __init__.py
file, which is the file that initializes the package. Here, have a look at this.
是的。您需要目录来包含__init__。py文件,它是初始化包的文件。来,看看这个。
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.
__init__。需要py文件使Python将目录视为包含包;这样做是为了防止使用公共名称(如字符串)的目录无意地隐藏稍后出现在模块搜索路径上的有效模块。最简单的情况是……py可以只是一个空文件,但是它也可以为包执行初始化代码,或者设置__all__变量,稍后将对此进行描述。
#11
4
Linux: Imported modules are located in /usr/local/lib/python2.7/dist-packages
Linux:导入的模块位于/usr/local/lib/python2.7/dist-packages中
If you're using a module compiled in C, don't forget to chmod the .so file after sudo setup.py install
.
如果您正在使用用C编译的模块,请不要忘记在sudo设置后对.so文件进行chmod。py安装。
sudo chmod 755 /usr/local/lib/python2.7/dist-packages/*.so
#12
3
In my case, the problem was I was linking to debug python
& boost::Python
, which requires that the extension be FooLib_d.pyd
, not just FooLib.pyd
; renaming the file or updating CMakeLists.txt
properties fixed the error.
在我的例子中,问题是我链接到debug python & boost:: python,这要求扩展为stupid b_d。pyd,不仅FooLib.pyd;重命名文件或更新CMakeLists。txt属性修正了错误。
#13
2
My two cents:
我的两个美分:
Spit:
随地吐痰:
Traceback (most recent call last):
File "bash\bash.py", line 454, in main
import bosh
File "Wrye Bash Launcher.pyw", line 63, in load_module
mod = imp.load_source(fullname,filename+ext,fp)
File "bash\bosh.py", line 69, in <module>
from game.oblivion.RecordGroups import MobWorlds, MobDials, MobICells, \
ImportError: No module named RecordGroups
This confused the hell out of me - went through posts and posts suggesting ugly syspath hacks (as you see my __init__.py
were all there). Well turns out that game/oblivion.py and game/oblivion was confusing python which spit out the rather unhelpful "No module named RecordGroups". I'd be interested in a workaround and/or links documenting this (same name) behavior -> EDIT (2017.01.24) - have a look at What If I Have a Module and a Package With The Same Name? Interestingly normally packages take precedence but apparently our launcher violates this.
这把我搞糊涂了——通过帖子和帖子暗示了丑陋的syspath黑客(正如你看到我的博客)。py都有)。结果是那个游戏/遗忘。py和game/oblivion混淆了python,这就产生了非常无用的“没有命名为RecordGroups的模块”。我想要一个解决方案和/或链接来记录这个(同名)行为——>编辑(2017.01.24)——看看如果我有一个模块和一个同名的包会怎么样?有趣的是,通常包优先,但显然我们的启动器违反了这个。
EDIT (2015.01.17): I did not mention we use a custom launcher dissected here.
编辑(2015.01.17):我没有提到我们在这里使用自定义启动器。
#14
1
If you have tried all methods provided above but failed, maybe your module has the same name as a built-in module. Or, a module with the same name existing in a folder that has a high priority in sys.path
than your module's.
如果您尝试了上面提供的所有方法,但是失败了,那么您的模块可能与内置模块同名。或者,在sys中具有高优先级的文件夹中存在同名的模块。比你的模块的路径。
To debug, say your from foo.bar import baz
complaints ImportError: No module named bar
. Changing to import foo; print foo
, which will show the path of foo
. Is it what you expect?
要调试,输入your from foo。bar进口baz投诉:没有模块命名bar。改变进口foo;打印foo,它会显示foo的路径。这是你所期望的吗?
If not, Either rename foo
or use absolute imports.
如果不是,要么重命名foo,要么使用绝对导入。
#15
0
After just suffering the same issue I found my resolution was to delete all pyc
files from my project, it seems like these cached files were somehow causing this error.
在遇到同样的问题之后,我发现我的解决方案是从我的项目中删除所有pyc文件,看起来这些缓存的文件在某种程度上导致了这个错误。
Easiest way I found to do this was to navigate to my project folder in Windows explorer and searching for *.pyc
, then selecting all (Ctrl+A) and deleting them (Ctrl+X).
我发现最简单的方法是在Windows资源管理器中导航到我的项目文件夹并搜索*。pyc,然后选择所有(Ctrl+A)并删除它们(Ctrl+X)。
Its possible I could have resolved my issues by just deleting the specific pyc
file but I never tried this
我可以通过删除特定的pyc文件来解决我的问题,但是我从来没有尝试过
#16
0
I faced the same problem: Import error
. In addition the library've been installed 100% correctly. The source of the problem was that on my PC 3 version of python (anaconda packet) have been installed). This is why the library was installed no to the right place. After that I just changed to the proper version of python in the my IDE PyCharm.
我遇到了同样的问题:导入错误。此外,该库已被100%正确安装。问题的根源是在我的PC 3版本的python (anaconda包)上已经安装了)。这就是为什么库被安装在正确的位置。之后,我在IDE PyCharm中修改了python的正确版本。
#17
0
Fixed my issue by writing print (sys.path)
and found out that python was using out of date packages despite a clean install. Deleting these made python automatically use the correct packages.
通过编写print (sys.path)解决了我的问题,并发现尽管安装很干净,python仍然在使用过期包。删除这些包使python自动使用正确的包。
#18
0
I had the same error. It was caused by somebody creating a folder in the same folder as my script, the name of which conflicted with a module I was importing from elsewhere. Instead of importing the external module, it looked inside this folder which obviously didn't contain the expected modules.
我犯了同样的错误。这是因为有人在我的脚本中创建了一个文件夹,这个名字与我从其他地方导入的模块冲突。它没有导入外部模块,而是查看了这个文件夹,显然不包含预期的模块。
#19
0
My problem was that I added the directory with the __init__.py
file to PYTHONPATH, when actually I needed to add its parent directory.
我的问题是我用__init__添加了目录。py文件到PYTHONPATH,实际上我需要添加它的父目录。
#20
0
I had the same problem (Python 2.7 Linux), I have found the solution and i would like to share it. In my case i had the structure below:
我遇到了同样的问题(Python 2.7 Linux),我找到了解决方案,我想与大家分享一下。在我的案例中,我有以下结构:
Booklet
-> __init__.py
-> Booklet.py
-> Question.py
default
-> __init_.py
-> main.py
In 'main.py' I had tried unsuccessfully all the combinations bellow:
在的主要。我试过所有的组合,都失败了。
from Booklet import Question
from Question import Question
from Booklet.Question import Question
from Booklet.Question import *
import Booklet.Question
# and many othet various combinations ...
The solution was much more simple than I thought. I renamed the folder "Booklet" into "booklet" and that's it. Now Python can import the class Question normally by using in 'main.py' the code:
解决办法比我想象的要简单得多。我把文件夹“小册子”改名为“小册子”,就是这样。现在Python可以通过使用in 'main来正常导入类问题。py的代码:
from booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass
From this I can conclude that Package-Names (folders) like 'booklet' must start from lower-case, else Python confuses it with Class names and Filenames.
从这里我可以得出这样的结论:包名(文件夹),比如'booklet'必须从小写开始,否则Python会混淆类名和文件名。
Apparently, this was not your problem, but John Fouhy's answer is very good and this thread has almost anything that can cause this issue. So, this is one more thing and I hope that maybe this could help others.
显然,这不是你的问题,但是John Fouhy的答案很好,这个线程几乎有任何东西可以引起这个问题。所以,这是另外一件事,我希望这能帮助其他人。
#1
206
Based on your comments to orip's post, I guess this is what happened:
根据你对奥里普的评论,我猜是这样的:
- You edited
__init__.py
on windows. - 你编辑__init__。py在windows。
- The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
- windows编辑器添加了一些非打印的东西,可能是一个传输返回(在windows中,行尾是CR/LF;在unix中,它仅仅是LF,或者可能是CTRL-Z (windows文件结束)。
- You used WinSCP to copy the file to your unix box.
- 您使用WinSCP将文件复制到您的unix框中。
- WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
- WinSCP认为:“这不是一个基本的文本;我用。bin扩展来表示二进制数据
- The missing
__init__.py
(now called__init__.py.bin
) means python doesn't understand toolkit as a package. - 失踪的__init__。py(现在称为__init__.py.bin)意味着python不理解工具包作为一个包。
- You create
__init__.py
in the appropriate directory and everything works... ? - 您创建__init__。py在适当的目录下,一切正常……吗?
#2
54
Does
做
(local directory)/site-packages/toolkit
have a __init__.py
?
有行吗?
To make import walk through your directories every directory must have a __init__.py
file.
要使导入遍历您的目录,每个目录必须有一个__init__。py文件。
#3
33
On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:
在*nix上,还要确保正确配置了PYTHONPATH,特别是它具有以下格式:
.:/usr/local/lib/python
(Mind the .:
at the beginning, so that it can search on the current directory, too.)
(注意:在开头,这样它也可以在当前目录上搜索。)
It may also be in other locations, depending on the version:
它也可能在其他地点,取决于版本:
.:/usr/lib/python
.:/usr/lib/python2.6
.:/usr/lib/python2.7 and etc.
#4
24
I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:
我在LPTHW做这个练习的时候遇到了一些非常相似的东西;我无法让Python识别我正在调用的目录中有文件。但我最终还是成功了。我所做的,也是我推荐的,就是尝试一下:
(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)
(注意:从您最初的文章中,我假设您使用的是基于* nix的机器,并且正在从命令行运行东西,因此本文的建议是针对此进行调整的。既然我运行Ubuntu,这就是我所做的)
1) Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the mountain.py
file, and trying to call the toolkit.interface.py
module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is the toolkit
directory.
1)将目录(cd)更改为文件所在目录上方的目录。在这种情况下,你是在试图跑山。py文件,并尝试调用toolkit.interface.py模块,它位于不同的目录中。在本例中,您将访问包含这两个文件的路径的目录(或者换句话说,这两个文件的路径共享的最近的目录)。这里是工具箱目录。
2) When you are in the tookit
directory, enter this line of code on your command line:
2)在tookit目录下,在命令行上输入这一行代码:
export PYTHONPATH=.
出口到PYTHONPATH =。
This sets your PYTHONPATH to ".", which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn't just look in your current directory, but in all the directories that are in your current directory).
这将把python路径设置为“。”,这基本上意味着您的PYTHONPATH现在将在您当前所在的目录中查找任何被调用的文件(更重要的是,在您所在目录的子目录分支中)。因此,它不仅在当前目录中查找,而且在当前目录中的所有目录中查找。
3) After you've set your PYTHONPATH in the step above, run your module from your current directory (the toolkit
directory). Python should now find and load the modules you specified.
3)在上面步骤中设置好python路径之后,从当前目录(工具包目录)运行模块。Python现在应该找到并加载您指定的模块。
Hope this helps. I was quite frustrated with this myself.
希望这个有帮助。我自己也很沮丧。
#5
21
I solved my own problem, and I will write a summary of the things that were wrong and the solution:
我解决了我自己的问题,我将写一份关于错误和解决方案的摘要:
The file needs to be called exactly __init__.py
. If the extension is different such as in my case .py.bin
then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.
文件需要被准确地称为__init__.py。如果扩展是不同的,比如我的。py。bin那么Python就不能在目录中移动,然后它就找不到模块。要编辑文件,您需要使用Linux编辑器,如vi或nano。如果你使用Windows编辑器,这将会写一些隐藏的字符。
Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do which python
, and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.
另一个影响它的问题是,我已经安装了另一个Python版本的根,所以如果某人正在使用Python的本地安装,请确保运行该程序的Python安装是本地Python。要检查这个,只需执行哪个python,并查看可执行文件是否位于本地目录中。如果没有,请更改路径,但是要确保本地Python目录比其他Python更早。
#6
13
To mark a directory as a package you need a file named __init__.py
, does this help?
要将一个目录标记为一个包,您需要一个名为__init__的文件。py,这有帮助吗?
#7
6
- You must have the file __ init__.py in the same directory where it's the file that you are importing.
- 你必须把文件保密。py在同一个目录中,它是您正在导入的文件。
- You can not try to import a file that has the same name and be a file from 2 folders configured on the PYTHONPATH.
- 您不能尝试导入具有相同名称的文件,并从在PYTHONPATH上配置的两个文件夹中作为一个文件。
eg: /etc/environment
例如:/etc/environment
PYTHONPATH=$PYTHONPATH:/opt/folder1:/opt/folder2
PYTHONPATH = $ PYTHONPATH:/ opt / folder1 / opt / folder2
/opt/folder1/foo
/ opt / folder1 / foo
/opt/folder2/foo
/ opt / folder2 / foo
And, if you are trying to import foo file, python will not know which one you want.
如果您正在尝试导入foo文件,python将不知道您想要哪个文件。
from foo import ... >>> importerror: no module named foo
从foo进口…没有命名为foo的模块
#8
6
Using PyCharm
(part of the JetBrains suite) you need to define your script directory as Source:Right Click > Mark Directory as > Sources Root
使用PyCharm (JetBrains套件的一部分),您需要定义您的脚本目录作为源:右击>标记目录作为>源根。
#9
6
an easy solution is to install the module using python -m pip install <library-name>
instead of pip install <library-name>
you may use sudo in case of admin restrictions
一个简单的解决方案是使用python -m pip安装
#10
5
Yup. You need the directory to contain the __init__.py
file, which is the file that initializes the package. Here, have a look at this.
是的。您需要目录来包含__init__。py文件,它是初始化包的文件。来,看看这个。
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.
__init__。需要py文件使Python将目录视为包含包;这样做是为了防止使用公共名称(如字符串)的目录无意地隐藏稍后出现在模块搜索路径上的有效模块。最简单的情况是……py可以只是一个空文件,但是它也可以为包执行初始化代码,或者设置__all__变量,稍后将对此进行描述。
#11
4
Linux: Imported modules are located in /usr/local/lib/python2.7/dist-packages
Linux:导入的模块位于/usr/local/lib/python2.7/dist-packages中
If you're using a module compiled in C, don't forget to chmod the .so file after sudo setup.py install
.
如果您正在使用用C编译的模块,请不要忘记在sudo设置后对.so文件进行chmod。py安装。
sudo chmod 755 /usr/local/lib/python2.7/dist-packages/*.so
#12
3
In my case, the problem was I was linking to debug python
& boost::Python
, which requires that the extension be FooLib_d.pyd
, not just FooLib.pyd
; renaming the file or updating CMakeLists.txt
properties fixed the error.
在我的例子中,问题是我链接到debug python & boost:: python,这要求扩展为stupid b_d。pyd,不仅FooLib.pyd;重命名文件或更新CMakeLists。txt属性修正了错误。
#13
2
My two cents:
我的两个美分:
Spit:
随地吐痰:
Traceback (most recent call last):
File "bash\bash.py", line 454, in main
import bosh
File "Wrye Bash Launcher.pyw", line 63, in load_module
mod = imp.load_source(fullname,filename+ext,fp)
File "bash\bosh.py", line 69, in <module>
from game.oblivion.RecordGroups import MobWorlds, MobDials, MobICells, \
ImportError: No module named RecordGroups
This confused the hell out of me - went through posts and posts suggesting ugly syspath hacks (as you see my __init__.py
were all there). Well turns out that game/oblivion.py and game/oblivion was confusing python which spit out the rather unhelpful "No module named RecordGroups". I'd be interested in a workaround and/or links documenting this (same name) behavior -> EDIT (2017.01.24) - have a look at What If I Have a Module and a Package With The Same Name? Interestingly normally packages take precedence but apparently our launcher violates this.
这把我搞糊涂了——通过帖子和帖子暗示了丑陋的syspath黑客(正如你看到我的博客)。py都有)。结果是那个游戏/遗忘。py和game/oblivion混淆了python,这就产生了非常无用的“没有命名为RecordGroups的模块”。我想要一个解决方案和/或链接来记录这个(同名)行为——>编辑(2017.01.24)——看看如果我有一个模块和一个同名的包会怎么样?有趣的是,通常包优先,但显然我们的启动器违反了这个。
EDIT (2015.01.17): I did not mention we use a custom launcher dissected here.
编辑(2015.01.17):我没有提到我们在这里使用自定义启动器。
#14
1
If you have tried all methods provided above but failed, maybe your module has the same name as a built-in module. Or, a module with the same name existing in a folder that has a high priority in sys.path
than your module's.
如果您尝试了上面提供的所有方法,但是失败了,那么您的模块可能与内置模块同名。或者,在sys中具有高优先级的文件夹中存在同名的模块。比你的模块的路径。
To debug, say your from foo.bar import baz
complaints ImportError: No module named bar
. Changing to import foo; print foo
, which will show the path of foo
. Is it what you expect?
要调试,输入your from foo。bar进口baz投诉:没有模块命名bar。改变进口foo;打印foo,它会显示foo的路径。这是你所期望的吗?
If not, Either rename foo
or use absolute imports.
如果不是,要么重命名foo,要么使用绝对导入。
#15
0
After just suffering the same issue I found my resolution was to delete all pyc
files from my project, it seems like these cached files were somehow causing this error.
在遇到同样的问题之后,我发现我的解决方案是从我的项目中删除所有pyc文件,看起来这些缓存的文件在某种程度上导致了这个错误。
Easiest way I found to do this was to navigate to my project folder in Windows explorer and searching for *.pyc
, then selecting all (Ctrl+A) and deleting them (Ctrl+X).
我发现最简单的方法是在Windows资源管理器中导航到我的项目文件夹并搜索*。pyc,然后选择所有(Ctrl+A)并删除它们(Ctrl+X)。
Its possible I could have resolved my issues by just deleting the specific pyc
file but I never tried this
我可以通过删除特定的pyc文件来解决我的问题,但是我从来没有尝试过
#16
0
I faced the same problem: Import error
. In addition the library've been installed 100% correctly. The source of the problem was that on my PC 3 version of python (anaconda packet) have been installed). This is why the library was installed no to the right place. After that I just changed to the proper version of python in the my IDE PyCharm.
我遇到了同样的问题:导入错误。此外,该库已被100%正确安装。问题的根源是在我的PC 3版本的python (anaconda包)上已经安装了)。这就是为什么库被安装在正确的位置。之后,我在IDE PyCharm中修改了python的正确版本。
#17
0
Fixed my issue by writing print (sys.path)
and found out that python was using out of date packages despite a clean install. Deleting these made python automatically use the correct packages.
通过编写print (sys.path)解决了我的问题,并发现尽管安装很干净,python仍然在使用过期包。删除这些包使python自动使用正确的包。
#18
0
I had the same error. It was caused by somebody creating a folder in the same folder as my script, the name of which conflicted with a module I was importing from elsewhere. Instead of importing the external module, it looked inside this folder which obviously didn't contain the expected modules.
我犯了同样的错误。这是因为有人在我的脚本中创建了一个文件夹,这个名字与我从其他地方导入的模块冲突。它没有导入外部模块,而是查看了这个文件夹,显然不包含预期的模块。
#19
0
My problem was that I added the directory with the __init__.py
file to PYTHONPATH, when actually I needed to add its parent directory.
我的问题是我用__init__添加了目录。py文件到PYTHONPATH,实际上我需要添加它的父目录。
#20
0
I had the same problem (Python 2.7 Linux), I have found the solution and i would like to share it. In my case i had the structure below:
我遇到了同样的问题(Python 2.7 Linux),我找到了解决方案,我想与大家分享一下。在我的案例中,我有以下结构:
Booklet
-> __init__.py
-> Booklet.py
-> Question.py
default
-> __init_.py
-> main.py
In 'main.py' I had tried unsuccessfully all the combinations bellow:
在的主要。我试过所有的组合,都失败了。
from Booklet import Question
from Question import Question
from Booklet.Question import Question
from Booklet.Question import *
import Booklet.Question
# and many othet various combinations ...
The solution was much more simple than I thought. I renamed the folder "Booklet" into "booklet" and that's it. Now Python can import the class Question normally by using in 'main.py' the code:
解决办法比我想象的要简单得多。我把文件夹“小册子”改名为“小册子”,就是这样。现在Python可以通过使用in 'main来正常导入类问题。py的代码:
from booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass
From this I can conclude that Package-Names (folders) like 'booklet' must start from lower-case, else Python confuses it with Class names and Filenames.
从这里我可以得出这样的结论:包名(文件夹),比如'booklet'必须从小写开始,否则Python会混淆类名和文件名。
Apparently, this was not your problem, but John Fouhy's answer is very good and this thread has almost anything that can cause this issue. So, this is one more thing and I hope that maybe this could help others.
显然,这不是你的问题,但是John Fouhy的答案很好,这个线程几乎有任何东西可以引起这个问题。所以,这是另外一件事,我希望这能帮助其他人。