PATH
中添加Python的位置pip install virtualenv
pip install virtualenvwrapper-win
C:\Users\XXXXX>pip list
Package Version
--------------------- -------
astroid 2.11.7
dill 0.3.5.1
distlib 0.3.6
filelock 3.8.0
isort 5.10.1
lazy-object-proxy 1.7.1
mccabe 0.7.0
pip 21.1.1
platformdirs 2.5.2
pylint 2.14.4
setuptools 56.0.0
tomli 2.0.1
tomlkit 0.11.1
typing-extensions 4.3.0
virtualenv 20.16.7
virtualenvwrapper-win 1.2.7
wrapt 1.14.1
mkvirtualenv env
lsvirtualenv
C:\Users\XXXX>mkvirtualenv venv
C:\Users\XXXX\Envs is not a directory, creating
created virtual environment CPython3.8.10.final.0-64 in 957ms
creator CPython3Windows(dest=C:\Users\CNYITON1\Envs\venv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\CNYITON1\AppData\Local\pypa\virtualenv)
added seed packages: pip==22.3.1, setuptools==65.5.1, wheel==0.38.4
activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
(venv) C:\Users\XXXX>lsvirtualenv
dir /b /ad "C:\Users\XXXX\Envs"
==============================================================================
venv
TIPS:默认创建的虚拟环境在
C盘用户文件夹的Envs
下,可以通过设置环境变量设置虚拟环境的路径:通过计算机–>属性–>高级系统设置–>环境变量–>在系统变量中新建“变量名”:WORKON_HOME
,变量值:“你自定义的路径”
rmvirtualenv <虚拟环境名称>
,如本文rmvirtualenv venv
workon <虚拟环境名称>
,如本文的workon venv
cdvirtualenv
deativate
C:\Users\XXXX>lsvirtualenv
dir /b /ad "D:\PyEnv"
==============================================================================
C:\Users\XXXX>mkvirtualenv venv
created virtual environment CPython3.8.10.final.0-64 in 616ms
creator CPython3Windows(dest=D:\PyEnv\venv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\CNYITON1\AppData\Local\pypa\virtualenv)
added seed packages: pip==22.3.1, setuptools==65.5.1, wheel==0.38.4
activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
(venv) C:\Users\XXXX>workon venv
(venv) C:\Users\XXXX>deactivate
C:\Users\XXXX>if defined _OLD_VIRTUAL_PYTHONPATH (set "PYTHONPATH=" )
print("hello world")
& : File D:\PyEnv\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:3
,原因见博客windows 终端无法加载文件 Scripts\Activate.ps1,因为在此系统上禁止运行脚本-更改执行策略PS D:\Workspace\09_Music> python -u "d:\Workspace\09_Music\test.py"
hello world!
PS D:\Workspace\09_Music> & D:/PyEnv/venv/Scripts/Activate.ps1
& : File D:\PyEnv\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:3
+ & D:/PyEnv/venv/Scripts/Activate.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
即可Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\XXXX> Set-ExecutionPolicy -Scope CurrentUser Unrestricted
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
workon <虚拟环境名称>
即可使用虚拟环境sudo apt-get install python3
sudo apt-get install python2
sudo apt-get install virtualenv
,安装成功的virtualenv能够直接在Linux命令窗口补全命令值得注意的是此时安装的virtualenv与在命令行输入python得到的版本是一致的(python3/pip3和python2/pip)
pc@pcv:~/share$ python
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
virtualenv <虚拟环境名称>
则会在当前文件夹下创建虚拟环境,如本文在桌面的vscode/pycode文件夹下创建了虚拟环境venv
:pc@pcv:~/Desktop/vscode/pycode$ virtualenv venv
created virtual environment CPython3.8.10.final.0-64 in 151ms
creator CPython3Posix(dest=/home/pc/Desktop/test/vpy, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/pc/.local/share/virtualenv)
added seed packages: pip==22.2.2, setuptools==63.4.1, wheel==0.37.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
source <虚拟环境的路径>/bin/activate
:如本文的 source /home/pc/Desktop/vscode/pycode/venv/bin/activate
,激活成功后在终端前面会出现(虚拟环境名称)pc@pcv:~/Desktop$ source /home/pc/Desktop/vscode/pycode/venv/bin/activate
(venv) pc@pcv:~/Desktop$
TIPS:vscode每次使用虚拟环境都需要激活一次,为了让启动时候默认加载虚拟环境,则将虚拟环境的
source
操作添加到用户环境中,即bashrc
文件下:echo "source /home/pc/Desktop/vscode/pycode/venv/bin/activate" >> ~/.bashrc
即可,然后重启Linux或者执行source ~/.bashrc
激活用户变量
deactivate
source <虚拟环境位置>/bin/activate
code .
在当前目录下打开VS codepython3 hello.py
运行脚本