python-虚拟环境搭建和使用(win10)

2022-09-11 08:49:49

强烈建议:

  1. 有什么不懂的,先去看文档!文档能解决几乎所有问题。千万不要遇到问题先csdn。
  2. Github的issue也挺有帮助的。

一、pyenv-win

https://github.com/pyenv-win/pyenv-win

1. 安装库

pip install pyenv-win --target %USERPROFILE%\.pyenv

2.NOTE: If you are running Windows 10 1905 or newer, you might need todisable the built-in Python launcher via Start > "Manage App Execution Aliases" and turning off the "App Installer" aliases for Python

3. windows powershell 设置环境变量

[System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
[System.Environment]::SetEnvironmentVariable('PYENV_ROOT',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
[System.Environment]::SetEnvironmentVariable('PYENV_HOME',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
[System.Environment]::SetEnvironmentVariable('path', $env:USERPROFILE + "\.pyenv\pyenv-win\bin;" + $env:USERPROFILE + "\.pyenv\pyenv-win\shims;" + [System.Environment]::GetEnvironmentVariable('path', "User"),"User")

4. 若有版本信息,则安装成功。

pyenv --version

使用pyenv管理版本

在terminal里操作

1. 下载所需版本的python。

pyenv install 3.9.6

2. 设置global的python版本。优先级:shell>local>global。

pyenv global 3.9.6

 3. 每次下载了什么库,或者更新了什么内容,都要rehash以下。

pyenv rehash

4. 下载所需要的库

pyenv exec pip install XXX

 二、下载virtualenv\virtualenvwrapper-win

进入pyenv的路径,python版本下对应的Scripts。之后就可以使用该版本的pip了。

pip install virtualenv
pip install virtualenvwrapper-win 

# 别忘了rehash

pyenv rehash

三、进入cmd,创建虚拟环境

# 仍然在之前的Scipts的路径下
# 使用virtualenvwrapper-win 进行虚拟环境创建

# 创建名称为BALA的虚拟环境,默认切换到环境目录。

mkvirtualenv BALA

# 如果要进入虚拟环境,则需要回到Scripts路径下。

workon BALA

# 之后就是在环境里了,就和base环境一样的啦。

会发现,在user文件夹下,Envs文件夹内,多出了一个BALA文件夹,这就是我们创建的环境。

四、VScode使用虚拟环境

Using Python Environments in Visual Studio Code

vscode的文档也好棒!爱了。

1. 选择环境

    最简单的方法:

  1. Ctrl + Shift + P
  2. 选择已经创建了的虚拟环境的interpreter

2.  vscode自动选定使用了。

After usingPython: Select Interpreter, that interpreter is applied when right-clicking a file and selectingPython: Run Python File in Terminal. The environment is also activated automatically when you use theTerminal: Create New Terminal command unless you change thepython.terminal.activateEnvironment setting tofalse.

  • 作者:siyi-024
  • 原文链接:https://blog.csdn.net/qq_30869745/article/details/121184689
    更新时间:2022-09-11 08:49:49