虚拟环境未激活:virtualenv未激活虚拟环境

3

我刚开始使用 virtualenv,对它并没有深入的了解。我按照网站上的指示创建了文件夹,并执行了 activate.ps1 文件。它没有出现任何错误,但是当我尝试使用python时,它仍然使用的是我的系统中安装的python而不是虚拟环境文件夹中的python。以下是我使用的命令:

PS A:\Code\IIITH\image-processing-iiith\SRIP> virtualenv venv
Using base prefix 'c:\\users\\shind\\appdata\\local\\programs\\python\\python37'
New python executable in A:\Code\IIITH\image-processing-iiith\SRIP\venv\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
PS A:\Code\IIITH\image-processing-iiith\SRIP> powershell -ExecutionPolicy ByPass -File venv\Scripts\activate.ps1
PS A:\Code\IIITH\image-processing-iiith\SRIP> python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Users\\shind\\AppData\\Local\\Programs\\Python\\Python37'

我使用了命令powershell -ExecutionPolicy ByPass -File venv\Scripts\activate.ps1来执行activate.ps1,因为正常执行时我遇到了一些安全错误。那么我的做法有什么问题吗?输出的可执行文件路径是系统中的路径,而不是venv文件夹中的可执行文件路径。此外,在执行pip install安装包时,它会显示满足要求。我应该怎么激活环境?
正常执行文件时我遇到的错误是:
PS A:\Code\IIITH\image-processing-iiith\SRIP>  venv\Scripts\activate.ps1
venv\Scripts\activate.ps1 : File A:\Code\IIITH\image-processing-iiith\SRIP\venv\Scripts\activate.ps1 cannot be loaded
because running scripts is disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:2
+  venv\Scripts\activate.ps1
+  ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

不要调用另一个PowerShell实例来运行文件,只需调用脚本:./venv/Scripts/Activate.ps1 - Maximilian Burszley
@ShantanuShinde,你正在使用哪个版本的Python? - lmiguelvargasf
@TheIncorrigible1,它给了我一些安全错误。我在问题描述中提到了这一点。 - Shantanu Shinde
@lmiguelvargasf Python 3.7.3 - Shantanu Shinde
1个回答

6

设置和使用虚拟环境:

PS /> python -m venv .venv
PS /> Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
PS /> ./.venv/Scripts/Activate.ps1
(.venv) PS /> pip install -r requirements.txt

[...]

(.venv) PS />

1
我按照以下步骤进行操作:
  1. 移动(cd - 更改目录)到项目目录
  2. 移动(cd - 更改目录)到由PyCharm创建的虚拟环境文件夹
  3. 运行Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
  4. .\Scripts\activate venv -- 然后成功激活了虚拟环境 希望这能对某些人有所帮助。
- ZKS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接