python - 我如何在运行时添加 python 包路径

标签 python

这是一个场景,希望你能帮助我。

我在 /home/k/PR-TEST/Pr/test 中有 python 单元测试

kahmed@qatools:~/PR-TEST/Pr-test$ ls
_apache.py        archive  clienttest  decorators.py   fakeTecoreClient.py  fixtures.pyc     psqlharness.py  servertest   testhelp.pyc  testutils.pyc
apache_server.py  cache    commontest  decorators.pyc  fixtures.py          mysqlharness.py  runtests.sh     testhelp.py  testutils.py

并让我实际针对@ /home/k/PR/Pr 运行测试的包命名为(目录命名)pref

kahmed@qatools:~/PR/Pr$ pwd
/home/kahmed/PR/Pr
kahmed@qatools:~/PR/Pr$ ls
bin  conf  images  init.d  iped  logrotate.d  NEWS  pref  rpa  sbin  scripts  src  test  VERSION

现在,我 cd 到 ~/PR-TEST/Pr-test,然后运行:

nosetests -s --with-coverage --cover-package /home/k/PR/Pr/pref  `find . -name "*test.py"`

但是我得到错误,它找不到 pref 中的包

知道如何解决这个问题吗?

我必须确保 python 环境知道包 pr

最佳答案

如果您不能(或不想)在启动程序之前修改 PYTHONPATH,您可以修改 sys.path在运行时期间实现类似的功能。
但在这种情况下,您需要包装对 nosetests 脚本的调用。另外请检查您的包是否存在区分大小写的问题 Prpr

关于python - 我如何在运行时添加 python 包路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5712460/

相关文章:

python - 在Python列表中查找 float

python - 打开网络浏览器后如何恢复程序(或退出)?

python - 将 Python AES 转换为 Node

python - 我可以在 selenium 运行时最小化 chrome 窗口吗?

python - 导入错误: cannot import name 'main' when trying to download any python library

Python PCA - 投影到低维空间

python - 在 for 循环中直接调用 SeqIO.parse() 可以,但是事先单独使用它不行吗?为什么?

Python 2.7 正则表达式捕获组未按预期工作

python - save() 收到意外的关键字参数

python - Psycopg2 使用 pip 安装有效但无法在 OS X 10.9 上导入模块