简体   繁体   English

使用venv时如何选择要运行的python可执行文件?

[英]How to choose which python executable to run when using venv?

I have a python installation on my pc (windows 10) which comes from Anaconda.我的电脑(Windows 10)上有一个来自 Anaconda 的 python 安装。 I am a data scientist and using conda as a package manager is very convenient for me.我是一名数据科学家,使用 conda 作为包管理器对我来说非常方便。

However, sometimes I want to develop a small app or script to share with my colleagues.但是,有时我想开发一个小应用程序或脚本与同事分享。 In these cases I create a project folder and python -m venv .venv inside it.在这些情况下,我会在其中创建一个项目文件夹和python -m venv .venv This way, I can install only the essential packages I need, and later share the requirements.txt file.这样,我可以只安装我需要的基本包,然后共享 requirements.txt 文件。

The issue I'm having, is that the python interpreter being used is still the default one, namely, the one that came with Anaconda, even if I activate the virtual environment and deactivate the conda one .我遇到的问题是,使用的 python 解释器仍然是默认的,即 Anaconda 附带的解释器,即使我激活了虚拟环境并停用了 conda one

Specifically, if I run python in the terminal, I get this warning message:具体来说,如果我在终端中运行 python,我会收到以下警告消息:

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

This is rather inconvenient.这比较不方便。 My base python installation is 3.7, but if I wanted to use an earlier version, or 3.8, I can't seem to be able to choose.我的基本python安装是3.7,但是如果我想使用更早的版本,或者3.8,我似乎无法选择。

I would expect that the python executable being used is the one in the current active environment, but this doesn't seem to be the case.我希望正在使用的 python 可执行文件是当前活动环境中的那个,但情况似乎并非如此。

How can I obtain that?我怎样才能得到它?

First you have to install the version of python you want to use in your venv.首先,您必须安装要在 venv 中使用的 python 版本。 It must already be available somewhere on your system to create a venv using it.它必须已经在您系统上的某个地方可用才能使用它创建 venv。

Then instead of just python -m venv .venv you specify which python with the full path : /path/to/pythonX.Y -m venv .venv然后,而不是仅仅python -m venv .venv你指定哪个 python 与完整路径: /path/to/pythonX.Y -m venv .venv

You can't have a venv that shares multiple versions of python, to my knowledge at least.至少据我所知,你不能拥有一个共享多个版本的 python 的 venv。

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

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