简体   繁体   English

从python项目中的不同包中调用模块

[英]Calling modules from different packages in a python Project

I am trying to wrap my head around the use of the __init__ file in python and I though I did it correctly but I am getting a "No module named ..." error. 我试图绕过在python中使用__init__文件的想法,尽管我正确地做到了,但是却收到“ No module named ...”错误。

I put the project here link to project for simplicity. 为了简单起见,我将项目链接到项目 The issue in question is pythonds/test/parens.py , I have __init__.py files in all my directories so I am not sure where I have gone wrong with this. 有问题的问题是pythonds/test/parens.py ,我的所有目录中都有__init__.py文件,因此我不确定在哪里出错。

I have also tried using PEP 238 using from ..basic.stack import Stack and get the error 我也尝试from ..basic.stack import Stack使用PEP 238并得到错误

ValueError: Attempted relative import in non-package

I though all I had to do was stick an __init__.py files into my directories to make them a module and then I would be able to call them from different modules. 尽管我要做的只是将__init__.py文件粘贴到目录中以使其成为模块,然后可以从不同的模块调用它们。

Within the __init__.py file, put: __init__.py文件中,放入:

from myfile import *

for instance: 例如:

pythonds/test/parens.py
pythonds/test/__init__.py

Where the __init__.py would contain: __init__.py将包含以下内容:

from parens import *

That's the dirty quick-fix version for older python versions. 那是旧python版本的肮脏的快速修复版本。 Considering you have multiple subdirectories something along the lines of (see blow) would be better for all python versions: 考虑到您有多个子目录,对于所有python版本,最好遵循(参见打击)的内容:

In your __init__.py in the folder test place: 在文件夹测试位置的__init__.py中:

__all__ = ['tacos','falaffels']
from test.tacos import factory
from test.falaffels import stand

If you have the following folder structure the above should work: 如果您具有以下文件夹结构,则上面的方法应该起作用:

main.py
/test/
    __init__.py
    tacos.py
    falaffels.py

Also just noticed that this might be a duplicate of: How do I write good/correct package __init__.py files 还只是注意到这可能是重复的: 我如何编写良好/正确的__init__.py包文件

Annoyingly, you elide the actual error: no module named what ? 烦人的是,您忽略了实际错误:没有模块命名为what If, as I suspect, it says "no module named 'pythonds', then the problem has nothing to do with __init__.py at all. It is simply that the base pythonds directory is not on your Python path, presumably because you're running the test from within its own directory. 如果我怀疑它说“没有名为'pythonds'的模块,那么问题根本与__init__.py无关。这仅仅是因为基本pythonds目录不在您的Python路径上,可能是因为您在从自己的目录中运行测试。

Python automatically puts the current directory in its path: so if ran the test from the pythonds directory you would be able to do from basic.stack import Stack . Python自动将当前目录放在其路径中:因此,如果从pythonds目录运行测试,则可以from basic.stack import Stack Otherwise, you could add the full path to the directory above parends to sys.path . 否则,您可以将完整路径添加到上面的目录parendssys.path

A couple of other tips: you probably already know that the collections module already defines a deque class, and lists can be easily used as stacks already. 其他一些技巧:您可能已经知道collections模块已经定义了一个双端队列类,并且列表可以很容易地用作堆栈。 But more to the point, note that it's not necessary in Python to define a separate file for each class: all three could live in the same stack.py file. 但更重要的是,请注意,在Python中没有必要为每个类定义单独的文件:这三个类都可以位于同一stack.py文件中。

Also, your tests aren't really tests. 另外,您的测试并不是真正的测试。 You should look into the unittest module for how to write proper unit tests in Python. 您应该查看unittest模块,以了解如何在Python中编写适当的单元测试。

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

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