Python virtualenv工具设置虚拟环境和VS code调试Python
- 1. Window环境下采用VS code调试Python和虚拟环境
- 1.1 安装Python
- 1.2 安装虚拟环境工具virtualenv
- 1.3 Windows上使用虚拟环境的基本操作
- 1.4 Windows上VS code使用虚拟环境
- 2. Linux环境下采用VS code调试Python和虚拟环境
- 2.1 Linux安装Python
- 2.2 Linux安装虚拟环境virtualenv
- 2.3 Linux上使用虚拟环境的基本操作
- 1.4 Linux上VS code使用虚拟环境
1. Window环境下采用VS code调试Python和虚拟环境
1.1 安装Python
-
- 安装网站:Python Releases for Windows
-
- 选择版本和位数:这里选择的是3.8.10的64安装版本
-
- 下载并安装Python:
-
- 在环境变量配置Python:只要在环境变量
PATH
中添加Python的位置
- 在环境变量配置Python:只要在环境变量
1.2 安装虚拟环境工具virtualenv
-
- 安装virtualenv:
pip install virtualenv
- 安装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
1.3 Windows上使用虚拟环境的基本操作
-
- 创建虚拟环境:
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=" )
1.4 Windows上VS code使用虚拟环境
-
- 在确保Python安装成功的情况下,安装VS code的Python插件
-
- 新建Python文件,并将解析器选择为虚拟环境的Python,输入测试代码
print("hello world")
- 新建Python文件,并将解析器选择为虚拟环境的Python,输入测试代码
-
- 报错的原因:直接运行会出现
& : 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
-
- 报错的解决办法:Windows打开power sheel,输入
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
即可
- 报错的解决办法:Windows打开power sheel,输入
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
-
- 退出VS code重新打开后再终端 再输入
workon <虚拟环境名称>
即可使用虚拟环境
- 退出VS code重新打开后再终端 再输入
2. Linux环境下采用VS code调试Python和虚拟环境
2.1 Linux安装Python
-
- 安装Python3:
sudo apt-get install python3
- 安装Python3:
-
- 安装Python2:
sudo apt-get install python2
- 安装Python2:
2.2 Linux安装虚拟环境virtualenv
-
- 采用apt命令:
sudo apt-get install virtualenv
,安装成功的virtualenv能够直接在Linux命令窗口补全命令
- 采用apt命令:
值得注意的是此时安装的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.
2.3 Linux上使用虚拟环境的基本操作
-
- 创建虚拟环境:
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
- 退出当前虚拟环境:
-
- 删除虚拟环境:则删除虚拟环境的文件夹即可
1.4 Linux上VS code使用虚拟环境
-
- 在终端中激活虚拟环境:
source <虚拟环境位置>/bin/activate
- 在终端中激活虚拟环境:
-
- 切换到项目文件夹下 输入
code .
在当前目录下打开VS code
- 切换到项目文件夹下 输入
-
- 和Windows操作系统一样,安装Python扩展插件
-
- VS code Python解析器选择为虚拟环境下的Python即可
-
- 打开终端输入
python3 hello.py
运行脚本
- 打开终端输入