Python virtualenv工具设置虚拟环境和VS code调试Python

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

    1. 安装网站:Python Releases for Windows
    1. 选择版本和位数:这里选择的是3.8.10的64安装版本

Python virtualenv工具设置虚拟环境和VS code调试Python_第1张图片

    1. 下载并安装Python:

Python virtualenv工具设置虚拟环境和VS code调试Python_第2张图片

    1. 在环境变量配置Python:只要在环境变量PATH中添加Python的位置

Python virtualenv工具设置虚拟环境和VS code调试Python_第3张图片
Python virtualenv工具设置虚拟环境和VS code调试Python_第4张图片

1.2 安装虚拟环境工具virtualenv

    1. 安装virtualenv:pip install virtualenv
    1. 安装虚拟环境管理工具:pip install virtualenvwrapper-win
    1. 安装完成如下所示
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上使用虚拟环境的基本操作

    1. 创建虚拟环境:mkvirtualenv env
    1. 查看虚拟环境: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,变量值:“你自定义的路径”
Python virtualenv工具设置虚拟环境和VS code调试Python_第5张图片

    1. 删除虚拟环境:rmvirtualenv <虚拟环境名称>,如本文rmvirtualenv venv
    1. 激活虚拟环境:workon <虚拟环境名称>,如本文的workon venv
    1. 进入虚拟环境的目录:cdvirtualenv
    1. 退出虚拟环境: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使用虚拟环境

    1. 在确保Python安装成功的情况下,安装VS code的Python插件

Python virtualenv工具设置虚拟环境和VS code调试Python_第6张图片

    1. 新建Python文件,并将解析器选择为虚拟环境的Python,输入测试代码print("hello world")

Python virtualenv工具设置虚拟环境和VS code调试Python_第7张图片

    1. 报错的原因:直接运行会出现& : 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
    1. 报错的解决办法:Windows打开power sheel,输入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
    1. 退出VS code重新打开后再终端 再输入workon <虚拟环境名称>即可使用虚拟环境

2. Linux环境下采用VS code调试Python和虚拟环境

2.1 Linux安装Python

    1. 安装Python3:sudo apt-get install python3
    1. 安装Python2:sudo apt-get install python2

2.2 Linux安装虚拟环境virtualenv

    1. 采用apt命令: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.

2.3 Linux上使用虚拟环境的基本操作

    1. 创建虚拟环境: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
    1. 激活虚拟环境: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激活用户变量

    1. 退出当前虚拟环境:deactivate
    1. 删除虚拟环境:则删除虚拟环境的文件夹即可

1.4 Linux上VS code使用虚拟环境

    1. 在终端中激活虚拟环境:source <虚拟环境位置>/bin/activate
    1. 切换到项目文件夹下 输入code .在当前目录下打开VS code
    1. 和Windows操作系统一样,安装Python扩展插件
    1. VS code Python解析器选择为虚拟环境下的Python即可
    1. 打开终端输入python3 hello.py运行脚本

Python virtualenv工具设置虚拟环境和VS code调试Python_第8张图片

你可能感兴趣的:(开发工具,python,virtualenv,开发语言)