virtualenv不激活虚拟环境

时间:2019-07-11 14:32:59

标签: python powershell virtualenv

我刚开始使用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

1 个答案:

答案 0 :(得分:2)

设置和使用虚拟环境:

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 />
相关问题