简体   繁体   English

虚拟环境和嵌入Python

[英]Virtual environments and embedding Python

I'm quite fond of Python's virtualenv , which facilitates maintenance of separate Python configurations. 我非常喜欢Python的virtualenv ,它有助于维护单独的Python配置。 I'm considering embedding Python into a C++ application and was wondering how the embedded Python would behave with respect to virtual environments. 我正在考虑将Python嵌入到C ++应用程序中,并且想知道嵌入式Python在虚拟环境方面的表现如何。

In particular, I'm intersted in knowing if it's possible to "select" a virtual environment based on some user-defined setting (eg by naming the virtual environment of interest in a configuration file). 特别是,我有兴趣知道是否可以基于某些用户定义的设置“选择”虚拟环境(例如,通过在配置文件中命名感兴趣的虚拟环境)。

The virtualenv documentation includes a Using virtualenv without bin/python section that hints at how to configure a virtual environment once the interpreter is already running. virtualenv文档包括一个不带bin/pythonUsing virtualenv部分,暗示了一旦解释器已经运行,如何配置虚拟环境。

To avoid hardcoding the path to the activate_this.py script, I use the following snippet: 为了避免硬编码activate_this.py脚本的路径,我使用以下代码段:

def resolve_virtual_environment(override=None):
    """Fetch the virtual environment path in the
       process' environment or use an override."""
    path = os.getenv('VIRTUAL_ENV')
    if override:
        path = os.path.join(os.getcwd(), override)
    return path

def activate_virtual_environment(environment_root):
    """Configures the virtual environment starting at ``environment_root``."""
    activate_script = os.path.join(
        environment_root, 'Scripts', 'activate_this.py')
    execfile(activate_script, {'__file__': activate_script})

And you can use it like so: 你可以像这样使用它:

if __name__ == '__main__':
    # use first argument is provided.
    override = None
    if len(sys.argv) > 1:
        override = sys.argv[1]
    environment_root = resolve_virtual_environment(override)

You can fetch the override value from a configuration file or something instead of from the command-line argument. 您可以从配置文件或其他内容中获取override值,而不是从命令行参数中获取。

Note that you can only still use a single virtual environment pre-process. 请注意,您仍然只能使用单个虚拟环境预处理。

Note : in contrast with using the interpreter bundled in the virtual environment, you have access to the packages installed for the interpreter you started. 注意 :与使用虚拟环境中捆绑的解释器相比,您可以访问为您启动的解释器安装的软件包。 For example, when using a globally-installed Python, you will have access to the globally-installed packages. 例如,使用全局安装的Python时,您将可以访问全局安装的软件包。

Also make sure that you use a Python interpreter with a version that matches whatever version you used to create the virtual environment to make sure that the standard library (copied into the virtual environment) version matches the Python interpreter version. 还要确保使用Python解释器,其版本与您用于创建虚拟环境的任何版本相匹配,以确保标准库(复制到虚拟环境)版本与Python解释器版本匹配。

Yeah, definitely. 是的,当然。 It's just a matter of where you set the PYTHONPATH to (or what you compile in). 这只是你将PYTHONPATH设置为(或编译的内容)的问题。

Make sure to check out pythonqt (not to be mistaken for PySide or PyQt .. it goes the other way, building a Python into a Qt C++ app. 一定要查看pythonqt (不要误认为是PySide或PyQt ..它是另一种方式,将Python构建到Qt C ++应用程序中。

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

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