简体   繁体   English

Python子模块导入差异

[英]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? 为什么会这样呢?

Code

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

Output 输出量

<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解释器中的pandasnumpy

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 这迫使它使用来自numpyrandom包而不是标准包

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.

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