虚拟环境问题 - 无法激活

256 投票
37 回答
658257 浏览
提问于 2025-04-17 10:35

我在我的项目周围创建了一个虚拟环境,但当我尝试激活它时却遇到了问题。

这可能只是语法或文件夹位置的问题,但我现在有点困惑。

你可以看到下面,我创建了一个虚拟环境,叫它venv。一切看起来都很好,然后我尝试通过运行 source venv/bin/activate 来激活它。

我在想这可能和我的系统路径有关,但不确定该指向哪里(我知道怎么编辑路径)。我使用的是Python 7 / Windows操作系统,虚拟环境版本是2.2.x。

Processing dependencies for virtualenv
Finished processing dependencies for virtualenv

c:\testdjangoproj\mysite>virtualenv --no-site-packages venv
The --no-site-packages flag is deprecated; it is now the default behavior.
Using real prefix 'C:\\Program Files (x86)\\Python'
New python executable in venv\Scripts\python.exe
File venv\Lib\distutils\distutils.cfg exists with different content; not overwri
ting
Installing setuptools.................done.
Installing pip...................done.

c:\testdjangoproj\mysite>source venv/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.

c:\testdjangoproj\mysite>source venv/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.

c:\testdjangoproj\mysite>source mysite/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.

c:\testdjangoproj\mysite>

37 个回答

43

我在我的Windows 10电脑上也遇到了同样的问题。

我尝试了以下步骤:

首先,打开anaconda终端。

步骤1

pip3 install -U pip virtualenv

步骤2

virtualenv --system-site-packages -p python ./venv

或者

virtualenv --system-site-packages -p python3 ./venv

步骤3

.\venv\activate

你可以通过anaconda里的spider工具来检查,输入 import tensorflow as tf

60

我也遇到过同样的问题。我当时使用的是Python 2,操作系统是Windows 10,使用的终端是Git Bash。结果发现,在Git Bash里你需要使用:

 source venv/Scripts/activate
585

source 是一个在 Linux(或者其他类似系统,但不包括 Windows)上使用的命令。

在 Windows 系统上,virtualenv 会创建一个 .bat 或 .ps1 文件,所以你应该运行 venv\Scripts\activate 来激活虚拟环境(具体可以参考 virtualenv 的 激活脚本文档)。

只需运行 activate,不需要加任何扩展名,这样无论你是在 cmd.exe 还是 PowerShell 中,都能正确使用这个文件。

撰写回答