[英]Python Submodule Import Discrepancy
I am dynamically importing the numpy package into a python environment of another proprietary system. 我正在将numpy包动态导入到另一个专有系统的python环境中。 The top level
numpy
package gets imported from the right place, but the numpy.random
package is pointing to the standard library. 顶级
numpy
包是从正确的位置导入的,但是numpy.random
包指向的是标准库。 Why is this happening? 为什么会这样呢?
import sys
LIB_PATH = 'T:\\Some\\Path\\'
if LIB_PATH not in sys.path:
sys.path.insert(0, LIB_PATH)
import numpy
print numpy
print numpy.random
<module 'numpy' from 'T:\Some\Path\numpy\__init__.pyc'>
<module 'random' from 'C:\Python26x64\Lib\random.pyc'>
Why is numpy.random
pointing to C:\\Python26x64\\Lib\\random.pyc
. 为什么
numpy.random
指向C:\\Python26x64\\Lib\\random.pyc
。 When I run this from my standard python interpreter at C:\\Python26x64\\
, then random is indeed the one in the numpy package 当我从
C:\\Python26x64\\
标准python解释器运行此代码时,numpy包中的确实是random。
Here is the solution that worked for me. 这是对我有用的解决方案。 It don't think its a good solution cause it requires changing one line of code in the
numpy
package. 它认为它不是一个好的解决方案,因为它需要更改
numpy
包中的一行代码。 Nonetheless, it allow us to use pandas
and numpy
from an embedded Python interpreter in a proprietary software by simply adding a folder to the sys.path
. 尽管如此,它允许我们通过将文件夹添加到
sys.path
来在专有软件中使用嵌入式Python解释器中的pandas
和numpy
。
File Changed 文件已更改
T:\\Some\\Path\\numpy\\__init__.py
Line Before 行前
Line: 171 - import random
行:171-
import random
This line assumes that the random
will be loaded from T:\\Some\\Path\\numpy\\random\\
该行假设
random
将被载入T:\\Some\\Path\\numpy\\random\\
Line After 线后
Line: 171 - from numpy import random
行:171-
from numpy import random
This forces it to use the random
package from numpy
instead of the standard package 这迫使它使用来自
numpy
的random
包而不是标准包
I thought I'd just leave there here and one of the un-accepted answer until someone can come up with a better solution. 我以为我会留在这里,这是一个无法接受的答案,直到有人可以提出更好的解决方案为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.