python 套件_Python套件?

在尝试执行Python测试模块时遇到了相对导入错误。问题在于直接运行测试文件导致无法进行相对导入。解决方案是将测试代码放在独立的脚本中,或者确保测试运行时PYTHONPATH包含项目根目录。此外,建议使用绝对导入,如`from euler import euler1`。
摘要由CSDN通过智能技术生成

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

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()

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

from .. import euler12

ValueError: Attempted relative import in non-package

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?

解决方案

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

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

Edit:

According to the Python docs, "modules intended for use as the main module of a Python application should always use absolute imports." (Cite)

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

Now, runtests.py can do from euler.tests.testeulern import TestCase and testeulern.py can do from .. import euler1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值