简体   繁体   English

对python导入感到困惑

[英]Confused about python imports

I reviewed the Python 2.7.5 documentation . 我查看了Python 2.7.5文档 I am having issues with my real project, but created a small test project here to concisely reproduce the issue. 我的真实项目有问题,但是在这里创建了一个小测试项目来简洁地重现问题。 Imagine a package with the following layout stored at ~/Development/Test 想象一个具有以下布局的程序包存储在〜/ Development / Test中

Here is the structure: 结构如下:

Test/
    __init__.py
    foo.py
    sub/
        __init__.py
        test_foo.py

And the code ( __init__.py files are empy): 和代码( __init__.py文件为empy):

foo.py foo.py

def bar():
    print("hello world")

test_foo.py test_foo.py

import Test.foo
# also tried from Test import foo

def main():
    foo.bar()

if __name__ == "__main__":
    main()

When trying to run test_foo.py from the terminal (ie python test_foo.py ) I'm getting: 当尝试从终端运行test_foo.py (即python test_foo.py )时,我得到了:

Traceback (most recent call last):
  File "test_foo.py", line 1, in <module>
    import Test.foo
ImportError: No module named Test.foo

I'm trying to import the main file in the package (foo.py) from the test file in the sub module (in my real project the sub module is the unit testing code). 我正在尝试从子模块中的测试文件导入包(foo.py)中的主文件(在我的真实项目中,子模块是单元测试代码)。 Oddly using Sublime text 2 editor and the plugin python test runner , I can run my individual tests just fine, but I cannot build the test file. 奇怪的是,使用Sublime text 2编辑器和插件python测试运行程序,我可以很好地运行单个测试,但无法构建测试文件。 It gives me the above error. 它给了我上面的错误。

Module names are case-sensitive. 模块名称区分大小写。 Use: 采用:

import Test.foo as foo

(The as foo is so that you can call foo.bar in main .) as foo是这样,因此您可以在main调用foo.bar 。)


You must also have ~/Development listed in PYTHONPATH. 您还必须在PYTHONPATH中列出~/Development

If using Unix and your login shell is bash, to add ~/Development to PYTHONPATH edit ~/.profile to include 如果使用Unix并且您的登录shell是bash,则将~/Development添加到PYTHONPATH编辑〜/ .profile以包括

export PYTHONPATH=$PYTHONPATH:$HOME/Development

Here are instructions for Windows . 这是Windows的说明


Further suggestions for debugging: 有关调试的其他建议:

Place 地点

import sys
print(sys.path)
import Test
print(Test)
import Test.foo

at the top of test_foo.py . test_foo.py的顶部。 Please post the output. 请发布输出。

Runnable scripts should always be put outside the module. 应将可运行脚本始终放在模块外部。 So if you have a module and a script that runs some code from that module, the structure should look like: 因此,如果您有一个模块和一个运行该模块中某些代码的脚本,则结构应类似于:

foo/
    __init__.py
    bar.py
your_script.py

And the code in your_script.py should be something like: 而且your_script.py的代码应类似于:

from foo.bar import your_func

your_func()

In case of unittesting it is a good idea (this is opinionated and everyone has his way of doing things) to place the tests inside the module so the structure should look like: 在进行单元测试的情况下,将测试放置在模块中一个好主意(这是有根据的,每个人都有自己的做事方式),因此结构应如下所示:

foo/
    __init__.py
    bar.py
    tests/
        test_bar.py

But in that case you shouldn't run the script directly. 但是在那种情况下,您不应该直接运行脚本。 You should either use one of the testing frameworks like nose and run: 您应该使用nose和运行这样的测试框架之一:

nosetests foo

in the directory where you placed your foo module. 在放置foo模块的目录中。

Or if you used the standard unittest library, create a TestSuite and what not, run it with: 或者,如果你使用的标准unittest库,创建一个TestSuite ,哪些不是,与运行:

python -m unittest foo.tests.test_bar

again in the directory where you placed your foo module. 再次在放置foo模块的目录中。

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

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