Python 解释为什么不应该从源目录导入numpy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14570011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Explain why numpy should not be imported from source directory
提问by Timothy Swan
Disclaimer of research:
研究免责声明:
I have examined the following other StackOverflow questions:
我检查了以下其他 StackOverflow 问题:
Perhaps to some, those may answer my question, but according to my knowledge, I still do not understand the situation.
也许对某些人来说,那些人可能会回答我的问题,但据我所知,我仍然不了解情况。
I am trying to import numpy so that matplotlib will work, but upon execution of the __init__.pyfile in the numpy folder, the following error message is displayed:
我正在尝试导入 numpy 以便 matplotlib 可以工作,但是在__init__.pynumpy 文件夹中执行文件时,会显示以下错误消息:
ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python intepreter from there.
Explain what it means to import something from its source directory as opposed to some other way of importing it. Does it mean that it should not be source code when it is imported? Or does it mean that it literally is just the wrong directory/folder that I am importing. I know that one other StackOverflow answer is:
解释从其源目录导入某些内容而不是其他某种导入方式的含义。是不是说导入的时候不应该是源代码?或者这是否意味着它实际上只是我正在导入的错误目录/文件夹。我知道另一个 StackOverflow 答案是:
The message is fairly self-explanatory; your working directory should not be the numpy source directory when you invoke Python; numpy should be installed and your working directory should be anything but the directory where it lives.
该消息是不言自明的。调用 Python 时,您的工作目录不应是 numpy 源目录;应该安装 numpy 并且您的工作目录应该不是它所在的目录。
However, I don't understand this. Aren't you supposed to import things that you want to work with? I'm assuming that the import command combines the source directory into your current working directory in this statement.
但是,我不明白这一点。你不应该导入你想要使用的东西吗?我假设 import 命令在此语句中将源目录合并到您当前的工作目录中。
I also read the other answers such as:
我还阅读了其他答案,例如:
Using
distutilsto install local directoriesUsing
virtualenvto create a virtual system directoryUsing Enthought's EPD to have numpy pre-installed in what I believe to be the system directory, and
Using a command like
$ dpkg -i --force-not-root --root=$HOME mypackagename.debto create what I believe is some kind of sub-system directory that is treated like a system directory.
使用
distutils安装本地目录使用
virtualenv创建虚拟系统目录使用 Enthought 的 EPD 在我认为是系统目录的地方预装了 numpy,并且
使用像这样的命令
$ dpkg -i --force-not-root --root=$HOME mypackagename.deb来创建我认为是某种被视为系统目录的子系统目录。
So, correct me if I'm wrong, but does numpy somehow strongly require to be somehow installed in the main system directory?
所以,如果我错了,请纠正我,但是 numpy 是否强烈要求以某种方式安装在主系统目录中?
Machine status:
机器状态:
I am using Windows machines without administrative privlidges.
They have Python 3.3 Shell as well as matplotlib installed.
When running command prompt, pythonand python3are not recognized. I have to run the Python shell from the applications menu.
I can successfull begin importing matplotlib from even my own directory, different from theirs, but it stops upon reaching __init__.pyof the numpy module, if it exists and reports the error stated above.
我正在使用没有管理权限的 Windows 机器。他们安装了 Python 3.3 Shell 和 matplotlib。运行命令提示符时,python并python3不能识别。我必须从应用程序菜单运行 Python shell。我可以成功地开始从我自己的目录导入 matplotlib,与他们的目录不同,但它在到达__init__.pynumpy 模块时停止,如果它存在并报告上述错误。
Update:
更新:
Luckily, my administrators were able to directly install numpy correctly in the site-packagesfolder. Thank you for answering my question though. I understand the situation a lot more because of you.
幸运的是,我的管理员能够直接在site-packages文件夹中正确安装 numpy 。不过还是谢谢你回答我的问题。因为你,我更了解情况了。
采纳答案by Robert Kern
numpy includes extension modules written in C. You will need to build these extension modules before the numpy package is complete. The most robust way to do this is to build it and install it to site-packageslike normal. You can also install it to another directory using the standard distutils options for this. However, once you have installed it, you should change your directory out of the source tree. Python starts looking for packages in your current directory, so the presence of the incomplete numpy package (without the necessary built C extension modules) will be picked up first and lead to the error that message that you quote. This happens a lot, so we give a long message explaining what to do.
numpy 包括用 C 编写的扩展模块。您需要在 numpy 包完成之前构建这些扩展模块。最可靠的方法是构建它并site-packages像往常一样安装它。您也可以使用标准的 distutils 选项将其安装到另一个目录。但是,一旦安装了它,就应该将目录从源代码树中更改出来。Python 开始在您的当前目录中查找包,因此将首先拾取不完整的 numpy 包(没有必要的内置 C 扩展模块)的存在,并导致您引用的错误消息。这种情况经常发生,所以我们给出了一个很长的消息来解释该怎么做。

