Python包? [英] Python Packages?
问题描述
好吧,我想无论我做错了什么,这可能都是显而易见的,但我无法弄清楚.我已经阅读并重新阅读了有关软件包的教程部分,我唯一能想到的是,这是行不通的,因为我直接执行了它.这是目录设置:
Ok, I think whatever I'm doing wrong, it's probably blindingly obvious, but I can't figure it out. I've read and re-read the tutorial section on packages and the only thing I can figure is that this won't work because I'm executing it directly. Here's the directory setup:
eulerproject/
__init__.py
euler1.py
euler2.py
...
eulern.py
tests/
__init__.py
testeulern.py
这是testeuler12.py(我编写的第一个测试模块)的内容:
Here are the contents of testeuler12.py (the first test module I've written):
import unittest
from .. import euler12
class Euler12UnitTests(unittest.TestCase):
def testtriangle(self):
"""
Ensure that the triangle number generator returns the first 10
triangle numbers.
"""
self.seq = [1,3,6,10,15,21,28,36,45,55]
self.generator = euler12.trianglegenerator()
self.results = []
while len(self.results) != 10:
self.results.append(self.generator.next())
self.assertEqual(self.seq, self.results)
def testdivisors(self):
"""
Ensure that the divisors function can properly factor the number 28.
"""
self.number = 28
self.answer = [1,2,4,7,14,28]
self.assertEqual(self.answer, euler12.divisors(self.number))
if __name__ == '__main__':
unittest.main()
现在,当我从目录中的IDLE和命令行执行此操作时,出现以下错误:
Now, when I execute this from IDLE and from the command line while in the directory, I get the following error:
Traceback (most recent call last):
File "C:\Documents and Settings\jbennet\My Documents\Python\eulerproject\tests\testeuler12.py", line 2, in <module>
from .. import euler12
ValueError: Attempted relative import in non-package
我认为问题在于,因为我直接运行它,所以我无法进行相对导入(因为__name__
发生了变化,而我对软件包描述的含糊理解是,__name__
是它说明方式的一部分
I think the problem is that since I'm running it directly, I can't do relative imports (because __name__
changes, and my vague understanding of the packages description is that __name__
is part of how it tells what package it's in), but in that case what do you guys suggest for how to import the 'production' code stored 1 level up from the test code?
推荐答案
通常,您会在PYTHONPATH上的某个位置找到一个目录,该目录的名称是您的软件包名称.例如:
Generally you would have a directory, the name of which is your package name, somewhere on your PYTHONPATH. For example:
eulerproject/
euler/
__init__.py
euler1.py
...
tests/
...
setup.py
然后,您可以在系统范围内安装此版本,也可以确保在调用脚本时设置PYTHONPATH=/path/to/eulerproject/:$PYTHONPATH
.
Then, you can either install this systemwide, or make sure to set PYTHONPATH=/path/to/eulerproject/:$PYTHONPATH
when invoking your script.
这样的绝对导入将起作用:
An absolute import like this will then work:
from euler import euler1
编辑:
根据Python文档,打算用作Python应用程序主模块的模块应始终使用绝对导入." (引用)
According to the Python docs, "modules intended for use as the main module of a Python application should always use absolute imports." (Cite)
因此另一个答案提到的像nose
这样的测试工具之所以起作用,是因为它导入软件包而不是从命令行运行它们.
So a test harness like nose
, mentioned by the other answer, works because it imports packages rather than running them from the command line.
如果您想手动执行操作,则可运行脚本需要位于程序包层次结构之外,如下所示:
If you want to do things by hand, your runnable script needs to be outside the package hierarchy, like this:
eulerproject/
runtests.py
euler/
__init__.py
euler1.py
...
tests/
__init__.py
testeulern.py
现在,runtests.py
可以执行from euler.tests.testeulern import TestCase
,testeulern.py
可以执行from .. import euler1
Now, runtests.py
can do from euler.tests.testeulern import TestCase
and testeulern.py
can do from .. import euler1
这篇关于Python包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!