简体   繁体   English

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

[英]How do i add a python package path during runtime

Here is a scenario, i hope you can help me with. 这是一个场景,我希望你能帮助我。

I have python unit tests in /home/k/PR-TEST/Pr/test 我在/home/k/PR-TEST/Pr/test进行了python单元/home/k/PR-TEST/Pr/test

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

and have the package that i am actually running tests against @ /home/k/PR/Pr named (directory named) pref 并让我实际运行的包对@ /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

Now, i cd to ~/PR-TEST/Pr-test , and run: 现在,我cd to ~/PR-TEST/Pr-test ,然后运行:

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

but i get erros that it cannot find the packages in pref 但我得到错误,它无法在pref中找到包

Any idea how to resolve this ? 知道如何解决这个问题吗?

I have to make sure that the python environment knows about the package pr 我必须确保python环境知道包pr

If you cannot (or do not want to) modify PYTHONPATH before you start the program, you can modify sys.path during runtime to achive similar functionality. 如果在启动程序之前不能(或不想)修改PYTHONPATH,则可以在运行时修改sys.path以实现类似的功能。
But in this case you will need to wrap the call to nosetests script. 但在这种情况下,您需要将调用包装到nosetests脚本中。 Also please check that you do not have case-sensitivity issue with your package being called Pr vs pr . 另外,请检查您的包裹是否具有区分大小写的问题,称为Pr vs pr

您应该将包的路径添加到PYTHONPATH环境变量

You should put your application package in a directory "foo/app" and the tests in another sub directory called "foo/tests". 您应该将您的应用程序包放在“foo / app”目录中,并将测试放在另一个名为“foo / tests”的子目录中。 Then run your tests from the foo directory so that they can import the app directly. 然后从foo目录运行测试,以便他们可以直接导入app

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

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