简体   繁体   English

从包外部导入 python 模块会产生意外行为

[英]importing python module from outside the package produce unexpected behavior

Let's say I have such a directory structure:假设我有这样一个目录结构:

 - Documents/
   - thesis_program/
     - __init__.py
     - classes.py
     - utils.py
     - GE_Test.py
   - GE_Test_fail.py

classes.py and utils.py contains some classes and functions. classes.py 和 utils.py 包含一些类和函数。 GE_Test.py and GE_Test_fail.py contains the exactly same code, except the import part. GE_Test.py 和 GE_Test_fail.py 包含完全相同的代码,除了导入部分。 In GE_Test.py I import classes and utils this way:GE_Test.py 中,我以这种方式导入类和工具:

from utils import execute
from classes import Grammatical_Evolution

While in GE_Test_fail.py , I import classes and utils this way:GE_Test_fail.py 中,我以这种方式导入类和工具:

from thesis_program.utils import execute
from thesis_program.classes import Grammatical_Evolution

And unexpectedly I get a different result.出乎意料的是,我得到了不同的结果。 Is there anything wrong here?这里有什么问题吗? Do I import the modules correctly?我是否正确导入模块?

I can ensure that the result should be the same, because I generate the random number with certain seed我可以确保结果应该是相同的,因为我生成了具有特定种子的随机数

Also classes.py is somehow depended on utils.py since I have several common functions in utils.py.此外 classes.py 以某种方式依赖于 utils.py,因为我在 utils.py 中有几个常用函数。 I suspect that utils is also a name used by the system.我怀疑 utils 也是系统使用的名称。 So in the second case (GE_Test_fail.py) The system utils override my utils.py.所以在第二种情况下(GE_Test_fail.py)系统工具覆盖了我的 utils.py。 But it doesn't seem make sense for me.但这对我来说似乎没有意义。

The complete source code of classes.py and utils.py is available here (if it helps to discover what's wrong): https://github.com/goFrendiAsgard/feature-extractor classes.py 和 utils.py 的完整源代码可在此处获得(如果它有助于发现问题所在): https : //github.com/goFrendiAsgard/feature-extractor

And also, the screenshots: https://picasaweb.google.com/111207164087437537257/November25201204?authuser=0&authkey=Gv1sRgCOKxot2a2fTtlAE&feat=directlink还有截图: https : //picasaweb.google.com/111207164087437537257/November25201204?authuser=0&authkey=Gv1sRgCOKxot2a2fTtlAE&feat=directlink

add below mentioned lines to your test files which are going outside of your thesis folder.将下面提到的行添加到您的论文文件夹之外的测试文件中。

import sys
sys.path.insert(0,"/path to your thesis folder/thesis_program")

and maintain everything else;并维护其他一切; for example in GE_Test.py .. .例如在GE_Test.py ..

import sys
sys.path.insert(0,"/path to your thesis folder/thesis_program")
from utils import execute
from classes import Grammatical_Evolution

EDIT:编辑:

Or use this to make it more dynamic或者用它来让它更有活力
(caution: don't try to find the path by os.path.abspath('./thesis_program') because it may not be always possible that you find your test_files and your thesis_folder are in the same dir; if you can fix them permanently in your code like above; then you are free to use them from anywhere on your system) (注意:不要尝试通过os.path.abspath('./thesis_program')查找路径,因为您可能无法始终找到test_filesthesis_folder位于同一目录中;如果您可以修复它们像上面一样永久保存在您的代码中;然后您可以在系统的任何地方自由使用它们)

import os, sys
lib_path = os.path.abspath('./thesis_program')
sys.path.insert(0,lib_path)

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

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