我刚开始使用 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
./venv/Scripts/Activate.ps1
。 - Maximilian Burszley