简体   繁体   English

使用路径中的句点将模块导入python

[英]Importing a module into python with a period in the path

I'm having trouble importing numpy into my script because of some path-name issues. 由于某些路径名问题,我无法将numpy导入到脚本中。

I'm running my script with python 2.7, rather than the default 2.6 on my server because I need some of the updates in the Collections module. 我正在使用python 2.7而不是服务器上的默认2.6运行python脚本,因为我需要Collections模块中的某些更新。 I cant seem to import numpy like: 我似乎无法像这样导入numpy:

from numpy.random import poisson

So I am trying to use the python2.7 specific links to numpy on my server, which are installed in: /opt/lib/python2.7/numpy 所以我试图使用python2.7特定的链接到我服务器上的numpy,这些链接安装在/opt/lib/python2.7/numpy中

But this period in the path is really making this difficult. 但是,这段时期确实使这变得困难。 I cannot change the path in anyway. 无论如何,我无法更改路径。

I've found a similar problem here , but frankly the code just doesn't make enough sense to me for me to feel safe using it (plus several commenters seemed to suggest it was a bad idea.). 我在这里发现了一个类似的问题,但是坦率地说,代码对我来说没有足够的意义,使我无法安全地使用它(加上一些评论者似乎暗示这是一个坏主意。)。 If someone has another suggestion, or if you can give me a good explanation of the code there I would appreciate it. 如果有人有其他建议,或者您可以给我很好的代码解释,我将不胜感激。

尝试将PYTHONPATH设置为指向/opt/lib/python2.7 ,然后import numpy等将其从那里拉出库。

$ PYTHONPATH=/opt/lib/python2.7 python27 my_script.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM