windows下安装pyenv+virtualenv+virtualenvwrapper

发布于:2025-03-04 ⋅ 阅读:(12) ⋅ 点赞:(0)

1、下载pyenv

进入git官网,打包下载zip到本地

2、解压到安装目录

解压下载好的pyenv-win-master.zip到自己的安装目录,如D:\Program Files

3、配置环境变量

右击桌面 此电脑 --> 属性  --> 高端系统设置 --> 环境变量 --> 新建系统变量

变量名:        PYENV

变量值:        D:\Program Files\pyenv-win-master

  • 作用PYENV 环境变量指向 pyenv-win 的安装目录。
  • 用途:这个变量帮助系统识别 pyenv 的安装路径,使得命令行可以正确调用 pyenv 工具。例如,如果 pyenv 安装在用户目录的某个位置,PYENV 变量会告诉操作系统 pyenv 所在的路径。

变量名:        PYENV_HOME

变量值:        D:\PythonEnv

  • 作用PYENV_HOME 指定了 pyenv 存储所有 Python 版本和环境的目录。
  • 用途:这个环境变量告诉 pyenv 要把下载和安装的 Python 版本存储在哪里。通常,PYENV_HOME 与 PYENV 环境变量指向的位置有关,但它专门用于存储和管理 Python 版本的文件。

编辑系统变量 Path,新增两行

%PYENV%\pyenv-win\bin

%PYENV%\pyenv-win\shims

4、验证是否安装成功

C:\Users\Administrator>pyenv --version
The system cannot find the file specified.
pyenv 3.1.1

5、查询可安装版本

C:\Users\Administrator>pyenv install --list | findstr 3.9
3.9.0a1-win32
3.9.0a1
3.9.0a2-win32
3.9.0a2
3.9.0a3-win32
3.9.0a3
3.9.0a4-win32
3.9.0a4
3.9.0a5-win32
...

# 先升级一下pyenv,我发现即使下载最新的,有可能也会找不到最新版本
C:\Users\Administrator>pyenv update
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Info] ::  Mirror: https://downloads.python.org/pypy/versions.json
:: [Info] ::  Mirror: https://api.github.com/repos/oracle/graalpython/releases
:: [Info] ::  Scanned 229 pages and found 810 installers.


C:\Users\Administrator>pyenv install 3.9.13
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Info] ::  Mirror: https://downloads.python.org/pypy/versions.json
:: [Info] ::  Mirror: https://api.github.com/repos/oracle/graalpython/releases
:: [Downloading] ::  3.9.13 ...
:: [Downloading] ::  From https://www.python.org/ftp/python/3.9.13/python-3.9.13-amd64.exe
:: [Downloading] ::  To   D:\Program Files\pyenv-win-master\pyenv-win\install_cache\python-3.9.13-amd64.exe

实际使用中会发现无法安装,因为下载不下来,使用国内源下载包之后放入D:\Program Files\pyenv-win-master\pyenv-win\install_cache目录下,重新执行安装语句就可以了。

C:\Users\Administrator>pyenv install 3.13.2
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Info] ::  Mirror: https://downloads.python.org/pypy/versions.json
:: [Info] ::  Mirror: https://api.github.com/repos/oracle/graalpython/releases
:: [Installing] ::  3.13.2 ...
:: [Info] :: completed! 3.13.2


C:\Users\Administrator>pyenv install 3.9.13
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Info] ::  Mirror: https://downloads.python.org/pypy/versions.json
:: [Info] ::  Mirror: https://api.github.com/repos/oracle/graalpython/releases
:: [Installing] ::  3.9.13 ...
:: [Info] :: completed! 3.9.13

6、查看已安装版本,设置全局版本

C:\Users\Administrator>pyenv versions
  3.13.2
  3.9.13

C:\Users\Administrator>pyenv global 3.13.2

C:\Users\Administrator>pyenv versions
* 3.13.2 (set by D:\Program Files\pyenv-win-master\pyenv-win\version)
  3.9.13

7、安装virtualenv

因为一个项目要配置一个虚拟环境,所以搭配virtualenv使用,当前在全局3.13.2版本下

C:\Users\Administrator>python -V
Python 3.13.2

C:\Users\Administrator>pip list
Package Version
------- -------
pip     24.3.1

C:\Users\Administrator>pip install virtualenv
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting virtualenv
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/93/fa/849483d56773ae29740ae70043ad88e068f98a6401aa819b5d6bee604683/virtualenv-20.29.2-py3-none-any.whl (4.3 MB)
Collecting distlib<1,>=0.3.7 (from virtualenv)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/91/a1/cf2472db20f7ce4a6be1253a81cfdf85ad9c7885ffbed7047fb72c24cf87/distlib-0.3.9-py2.py3-none-any.whl (468 kB)
Collecting filelock<4,>=3.12.2 (from virtualenv)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/89/ec/00d68c4ddfedfe64159999e5f8a98fb8442729a63e2077eb9dcd89623d27/filelock-3.17.0-py3-none-any.whl (16 kB)
Collecting platformdirs<5,>=3.9.1 (from virtualenv)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/3c/a6/bc1012356d8ece4d66dd75c4b9fc6c1f6650ddd5991e421177d9f8f671be/platformdirs-4.3.6-py3-none-any.whl (18 kB)
Installing collected packages: distlib, platformdirs, filelock, virtualenv
Successfully installed distlib-0.3.9 filelock-3.17.0 platformdirs-4.3.6 virtualenv-20.29.2

[notice] A new release of pip is available: 24.3.1 -> 25.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip

8、安装virtualenvwrapper-win

C:\Users\Administrator>pip install virtualenvwrapper-win
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting virtualenvwrapper-win
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/76/c5/87ee8d40722d28b44bd195f7b5679be41280d4df9dbf54597b95583e1f6d/virtualenvwrapper_win-1.2.7-py3-none-any.whl (18 kB)
Requirement already satisfied: virtualenv in d:\program files\pyenv-win-master\pyenv-win\versions\3.13.2\lib\site-packages (from virtualenvwrapper-win) (20.29.2)
Requirement already satisfied: distlib<1,>=0.3.7 in d:\program files\pyenv-win-master\pyenv-win\versions\3.13.2\lib\site-packages (from virtualenv->virtualenvwrapper-win) (0.3.9)
Requirement already satisfied: filelock<4,>=3.12.2 in d:\program files\pyenv-win-master\pyenv-win\versions\3.13.2\lib\site-packages (from virtualenv->virtualenvwrapper-win) (3.17.0)
Requirement already satisfied: platformdirs<5,>=3.9.1 in d:\program files\pyenv-win-master\pyenv-win\versions\3.13.2\lib\site-packages (from virtualenv->virtualenvwrapper-win) (4.3.6)
Installing collected packages: virtualenvwrapper-win
Successfully installed virtualenvwrapper-win-1.2.7

[notice] A new release of pip is available: 24.3.1 -> 25.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip

9、配置字virtualenv的环境变量

因为使用pyenv时会有多个python的解释器,而且安装的python并没有添加环境变量,这里需要手动添加,我们通过pyenv安装的所有的python都存放在 %PYENV%/pyenv-win\versions下

编辑环境变量 Path,添加

D:\Program Files\pyenv-win-master\pyenv-win\versions\3.13.2\Scripts

添加后安装虚拟环境env1 env2,发现可以 指定我们安装过的所有版本的虚拟环境。 

C:\Users\Administrator\Envs> mkvirtualenv env1
created virtual environment CPython3.13.2.final.0-64 in 1071ms
  creator CPython3Windows(dest=C:\Users\Administrator\Envs\env1, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, via=copy, app_data_dir=C:\Users\Administrator\AppData\Local\pypa\virtualenv)
    added seed packages: pip==25.0.1
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

C:\Users\Administrator\Envs>mkvirtualenv -p "D:\Program Files\pyenv-win-master\pyenv-win\versions\3.9.13\python.exe" env2
created virtual environment CPython3.9.13.final.0-64 in 2053ms
  creator CPython3Windows(dest=C:\Users\Administrator\Envs\env2, 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\Administrator\AppData\Local\pypa\virtualenv)
    added seed packages: pip==24.2, setuptools==72.0.0, wheel==0.43.0
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

 但是默认情况下,虚拟环境是安装到C:\Users\Administrator\Envs(用户目录下的)

修改virtualenv的虚拟环境目录

先删除安装的虚拟环境

C:\Users\Administrator>workon

Pass a name to activate one of the following virtualenvs:
==============================================================================
env1
env2

C:\Users\Administrator>rmvirtualenv env1

    Deleted C:\Users\Administrator\Envs\env1


C:\Users\Administrator>rmvirtualenv env2

    Deleted C:\Users\Administrator\Envs\env2

配置环境变量

新建系统变量

变量名:WORKON_HOME

变量值:D:\PythonEnv (自定义)

重新打开cmd,重新创建虚拟环境

D:\PythonEnv>mkvirtualenv env1
created virtual environment CPython3.13.2.final.0-64 in 623ms
  creator CPython3Windows(dest=D:\PythonEnv\env1, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, via=copy, app_data_dir=C:\Users\Administrator\AppData\Local\pypa\virtualenv)
    added seed packages: pip==25.0.1
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator


D:\PythonEnv>mkvirtualenv -p "D:\Program Files\pyenv-win-master\pyenv-win\versions\3.9.13\python.exe" env2
created virtual environment CPython3.9.13.final.0-64 in 1238ms
  creator CPython3Windows(dest=D:\PythonEnv\env2, 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\Administrator\AppData\Local\pypa\virtualenv)
    added seed packages: pip==25.0.1, setuptools==75.8.0, wheel==0.45.1
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

 10、删除

依次重后往前删除已配置的环境变量、删除D:\PythonEnv、删除D:\Program Files\pyenv-win-master安装目录