使用Virtualenv在Windows上安装多个Python版本

You are here because:

您在这里是因为:

  1. You are using Windows OS version 10+

    您正在使用Windows OS版本10+
  2. You would like to use multiple Python versions on the same computer

    您想在同一台计算机上使用多个Python版本
  3. You are tired of the internet telling you to “Just Use Virtualenv”

    您已经厌倦了互联网告诉您“仅使用Virtualenv”

TL; DR (TL;DR)

  1. Open Command Prompt and enter pip install virtualenv

    打开Command Prompt然后输入pip install virtualenv

  2. Download the desired python version (do NOT add to PATH!), and remember the path\to\new_python.exe of the newly installed version

    下载所需的python版本(不要添加到PATH!),并记住新安装版本的path\to\new_python.exe

  3. To create a virtualenv, open Command Prompt and enter

    要创建一个virtualenv,请打开Command Prompt并输入

    To create a virtualenv, open Command Prompt and enter virtualenv \path\to\env -p path\to\new_python.exe

    要创建virtualenv,请打开Command Prompt然后输入virtualenv \path\to\env -p path\to\new_python.exe

  4. If you are using PyCharm, update the Project Interpreter and the Code compatibility inspection.

    如果您使用的是PyCharm ,请更新Project InterpreterCode compatibility inspection

  5. To install packages:

    要安装软件包:

    (I) Activate virtualenv: open

    (一)激活virtualenv:打开

    Command Prompt and enter path\to\env\Scripts\activate.bat

    Command Prompt并输入path\to\env\Scripts\activate.bat

    (II)  Install desired packages

    (II)安装所需的软件包

    (III)  Deactivate with

    (III)用

    deactivate .

    deactivate

加长版; 阅读 (The Long version; Do Read)

序幕 (Prologue)

If you are using the Anaconda App this process might be easier using their GUI. I haven’t tried it myself, please let me know how it went if you are going down that road :)

如果您正在使用Anaconda应用程序,则使用其GUI可能会更容易。 我自己还没有尝试过,如果您沿着这条路走,请告诉我它是怎么回事:)

1.安装virtualenv (1. Install virtualenv)

If you already have some virtual environments, or are using Anaconda, make sure the next steps are performed from outside all these environments.

如果您已经有一些虚拟环境,或者正在使用Anaconda,请确保从所有这些环境之外执行下一步。

2.安装Python (2. Install Python)

You can download python from the official site, for example for python3.7.3 go here.

您可以从官方站点下载python,例如python3.7.3请转到此处

The file you should be downloading is called Windows x86–64 executable installer, or Windows x86 executable installer if for some reason you are using a 32-bit windows.

您应该下载的文件称为Windows x86–64 executable installer ,如果由于某些原因使用的是32位Windows,则称为Windows x86–64 executable installer Windows x86 executable installer

Once downloading is finished, open the executable file and an installation prompt will appear.

下载完成后,打开可执行文件,将出现安装提示。

  • You do NOT want to add the new python to your PATH since we are going to have multiple python versions on the same computer, and we would like for each application to know only one python version.

    您不希望将新的python添加到您的PATH中,因为我们将在同一台计算机上拥有多个python版本,并且我们希望每个应用程序仅知道一个python版本。
  • Either use the default suggested location for the new python, or supply a location of your choice. Either way, remember this location and let’s denote it from now on with C:\<some_path>\Python37 .

    为新python使用默认的建议位置,或提供您选择的位置。 无论哪种方式,请记住此位置,并从现在开始使用C:\<some_path>\Python37表示它。

3.创建一个virtualenv (3. Create a virtualenv)

Open the Command Prompt, or if you are using Anaconda open the Anaconda Prompt .

打开Command Prompt ,或者如果您正在使用Anaconda,则打开Anaconda Prompt

Decide where you want your virtualenv to be, for example, C:\Users\<your_username>\Anaconda3\envs\<env_name> .

确定您希望virtualenv的位置,例如C:\Users\<your_username>\Anaconda3\envs\<env_name>

Enter:

输入:

virtualenv C:\Users\<your_username>\Anaconda3\envs\<env_name> -p C:\<some_path>\Python37\python.exe

virtualenv C:\Users\<your_username>\Anaconda3\envs\<env_name> -p C:\<some_path>\Python37\python.exe

4.更新PyCharm解释器 (4. Update PyCharm Interpreter)

If you are using PyCharm, open the project you would like to work on (that is/will be written with the new python version), and go to File -> Settings -> Project -> Project Interpreter press the gear icon and then Add.. .

如果您使用的是PyCharm,请打开您要处理的项目(即将使用新的python版本编写的项目),然后依次转到File -> Settings -> Project -> Project Interpreter按齿轮图标,然后Add..

This will open a prompt window that allows you to define a new interpreter:

这将打开一个提示窗口,允许您定义新的解释器:

Assuming you are using Code Inspections, you might need to tell PyCharm which python version to inspect for. Go to File -> Settings-> Editor -> Inspections -> Python -> Code compatibility Inspection , make sure the box on the top indicates the specific project you are working on, and tick the box of your python version.

假设您正在使用代码检查,则可能需要告诉PyCharm要检查哪个python版本。 转到“ File -> Settings-> Editor -> Inspections -> Python -> Code compatibility Inspection ,确保顶部的框指示您正在处理的特定项目,然后选中python版本的框。

5.安装软件包 (5. Install packages)

Currently, your virtualenv contains only the crucial packages, pip and setuptools . To install more packages:

当前,您的virtualenv仅包含关键软件包pipsetuptools 。 要安装更多软件包:

  1. Open Command Prompt or Anaconda Prompt , and activate your virtualenv by entering

    打开Command PromptAnaconda Prompt ,然后通过输入激活您的virtualenv

    Open Command Prompt or Anaconda Prompt , and activate your virtualenv by entering C:\Users\<your_username>\Anaconda3\envs\<env_name>\activate.bat

    打开Command PromptAnaconda Prompt ,然后通过输入C:\Users\<your_username>\Anaconda3\envs\<env_name>\activate.bat您的C:\Users\<your_username>\Anaconda3\envs\<env_name>\activate.bat 激活您的virtualenv。

  2. Use pip to install packages like you usually do.

    使用pip像通常一样安装软件包。

  3. Deactivate your virtualenv by entering deactivate .

    通过输入deactivate 停用您的virtualenv。

结语 (Epilogue)

This morning, when I decided to open a new project with a different python version, I thought, “Yeah, I’ll just use a virtualenv”, because the internet said I can “Just do it”.

今天早上,当我决定用不同的python版本打开一个新项目时,我想:“是的,我将只使用virtualenv”,因为互联网上说我可以“做到这一点”。

Well, it’s working now, so no hard feelings dear internet, but seriously, was the “Just” really justified? Does reinstalling-PyCharm-only-because-I-want-to-have-proper-code-inspections fall under the “Just” category??

好吧,它现在正在运行,所以没有难过的感觉,亲爱的互联网,但是说真的,“ Just”真的合理吗? 仅因为我想拥有适当的代码检查而仅重新安装PyCharm,是否属于“仅”类别?

Anyway, along the way I stumbled upon several helpful guides, but each one took me “just” one step of the way, so I decided to put it all in one place.

无论如何,在此过程中,我偶然发现了几条有用的指南,但每条指南仅使我“一步一步”走了,所以我决定将其全部放在一个地方。

I hope my journey helped you with yours, and may we all enjoy happy coding, with as little as IT-friction as possible :D

希望我的旅程对您有所帮助,并希望我们所有人都可以享受快乐的编码,并尽可能减少IT摩擦:D

翻译自: https://www.freecodecamp.org/news/installing-multiple-python-versions-on-windows-using-virtualenv/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值