简体   繁体   English

如何为独立的python应用程序使用PYTHONPATH

[英]how to use PYTHONPATH for independent python application

Can anyone let me know how to set PYTHONPATH? 谁能让我知道如何设置PYTHONPATH?

Do we need to set it in the environment variables (is it system specific) or we can independently set the PYTHONPATH and use it to run any independent python application? 我们是否需要在环境变量中设置它(它是系统特定的)还是可以独立设置PYTHONPATH并使用它运行任何独立的python应用程序?

i need to pick the module from a package available in directory which is different from the directory from which I am running my application . 我需要从目录中提供的软件包中选择模块,该软件包与我正在运行应用程序的目录不同。 How to include these packages in my application 如何在我的应用程序中包含这些软件包

I assume you are using Linux 我假设您使用的是Linux

Before executing your application u can metion pythonpath=path && execution script 在执行您的应用程序之前,您可以指定pythonpath = path &&执行脚本

Other elegant way is using virtualenv. 另一种优雅的方法是使用virtualenv。 Where u can have diff packages for each application. 您可以在其中为每个应用程序提供差异包。 Before exection say workon env and then deactivate 执行之前,先说一下workon env,然后停用

Python3 has virtualenv by default Python3默认具有virtualenv

Instead of messing with system environment variables and paths set therein, if you only need to add a specific directory to the module lookup path of your application use: 如果只需要在应用程序使用的模块查找路径中添加特定目录,则无需弄乱系统环境变量和其中设置的路径:

import sys

sys.path.append("path/to/your/packages")

It's not recommended to mess with your paths in general - if you have to, you should probably rethink your application design - but if you still decide that you have to, this is more preferable than messing with system paths. 不建议一般情况下弄乱您的路径-如有必要,您可能应该重新考虑应用程序设计-但是如果您仍然决定必须这样做,则比弄乱系统路径更可取。

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

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