【发布时间】:2018-03-30 10:01:45
【问题描述】:
我在使用 tox 时遇到了一些麻烦。测试无法运行,因为无法找到被测模块。从我所做的挖掘来看,我怀疑问题可能是 tox 正在使用错误的解释器运行测试。如果相关的话,我正在使用 Windows。
找不到包,即使它已经安装 进入venv:
C:\Users\galli\Desktop\projects\RedditBotBuilder>tox
GLOB sdist-make: C:\Users\galli\Desktop\projects\RedditBotBuilder\setup.py
py36 inst-nodeps: C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\dist\RedditBotBuilder-1.0.0.zip
py36 installed: attrs==17.4.0,certifi==2018.1.18,chardet==3.0.4,colorama==0.3.9,idna==2.6,more-itertools==4.1.0,pluggy==0.6.0,praw==5.4.0,prawcore==0.14.0,py==1.5.3,pytest==3.5.0,RedditBotBuilder==1.0.0,requests==2.18.4,six==1.11.0,update-checker==0.16,urllib3==1.22
py36 runtests: PYTHONHASHSEED='150'
py36 runtests: commands[0] | pytest --verbose tst/
============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-3.5.0, py-1.5.3, pluggy-0.6.0 -- c:\users\galli\desktop\projects\redditbotbuilder\.tox\py36\scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\galli\Desktop\projects\RedditBotBuilder, inifile:
collected 0 items / 1 errors
=================================== ERRORS ====================================
_____________ ERROR collecting tst/redditbotbuilder/test_bots.py ______________
ImportError while importing test module 'C:\Users\galli\Desktop\projects\RedditBotBuilder\tst\redditbotbuilder\test_bots.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tst\redditbotbuilder\test_bots.py:3: in <module>
from redditbotbuilder.bots import RedditBot
E ModuleNotFoundError: No module named 'redditbotbuilder.bots'
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.22 seconds ===========================
ERROR: InvocationError: 'C:\\Users\\galli\\Desktop\\projects\\RedditBotBuilder\\.tox\\py36\\Scripts\\pytest.EXE --verbose tst/'
______________________________________________________ summary _______________________________________________________
ERROR: py36: commands failed
这是目录结构,我已将项目导入 PyCharm,它可以毫无问题地解析模块:
src/
src/redditbotbuilder/
src/redditbotbuilder/bots.py
src/redditbotbuilder/__init__.py
tst/
tst/redditbotbuilder/
tst/redditbotbuilder/test_bots.py
tst/redditbotbuilder/__init__.py
tox.ini
setup.py
这里是setup.py:
setup(
name="RedditBotBuilder",
description="A Python framework for quickly building reddit bots",
version="1.0.0",
url="someUrl.com",
author="Guy McGuyerson",
author_email="guymcguyerson@gmail.com",
classifiers=[
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries :: Python Modules"
],
packages=find_packages("src"),
package_dir={'': 'src'},
install_requires=["praw"]
)
这里是tox.ini:
[tox]
envlist = py36
[testenv]
deps=pytest
commands=pytest --verbose tst/
RedditBotBuilder 已安装在 venv 中:
(py36) C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\py36\Scripts>pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
attrs (17.4.0)
certifi (2018.1.18)
chardet (3.0.4)
colorama (0.3.9)
idna (2.6)
more-itertools (4.1.0)
pip (9.0.3)
pluggy (0.6.0)
praw (5.4.0)
prawcore (0.14.0)
py (1.5.3)
pytest (3.5.0)
RedditBotBuilder (1.0.0) <----------
requests (2.18.4)
setuptools (39.0.1)
six (1.11.0)
update-checker (0.16)
urllib3 (1.22)
wheel (0.30.0)
我认为可能是使用了错误的解释器...
(py36) C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\py36\Scripts>where python
C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\py36\Scripts\python.exe
C:\Users\galli\AppData\Local\Programs\Python\Python36-32\python.exe
...但是在将语句import sys; print(sys.executable) 添加到测试文件后,它打印了C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\py36\Scripts\python.exe。
【问题讨论】:
-
您的
setup.py文件是什么样的? -
要导入
redditbotbuilder.bots,必须有src/redditbotbuilder/__init__.py。有吗? -
对不起,是的,我忘了提到
src/redditbotbuilder/__init__.py和tst/redditbotbuilder/__init__.py都存在。当我可以访问我的家用电脑时(可能需要几个小时),我会发布setup.py,感谢您的帮助。 -
我在帖子中添加了
setup.py。 -
还添加了一些调试输出,看来毕竟使用了正确的解释器。