简体   繁体   English

一台电脑有两个版本的python

[英]Two versions of python in one computer

I have Pyhton 3.8 (32-bit) in my computer, I use Pycharm and also jupyter notebook.我的计算机中有 Pyhton 3.8(32 位),我使用 Pycharm 和 jupyter 笔记本。 I need to install Python 3.6 (64-bit) because I need to work with tensorflow and it only works with Python 3.6(64-bit)我需要安装 Python 3.6(64 位),因为我需要使用 tensorflow 并且它仅适用于 Python 3.6()

Can I have two versions of python together?我可以同时拥有两个版本的 python 吗? And How can change and work with two python versions in pycharm and jupyter?以及如何在 pycharm 和 jupyter 中更改和使用两个 python 版本?

Yes you can have multiple Python versions.是的,您可以拥有多个 Python 版本。 You can add dependency like python3.6 .您可以添加依赖项,例如python3.6

You can also use pyenv .您也可以使用pyenv It provides excellent facility to manage various python versions on your machine.它提供了出色的工具来管理您机器上的各种 python 版本。

The preferred way to use python virtual environments now is venv .现在使用 python 虚拟环境的首选方式是venv

You can install any number of python versions on your windows, though please also install the py launcher that it comes with.您可以在 windows 上安装任意数量的 python 版本,但也请安装它随附的 py 启动器。 This makes it easy to launch whichever python version you want through the commandline, with no need to rely on PATH nonsense.这使得通过命令行轻松启动您想要的任何 python 版本,无需依赖 PATH 废话。

If you've the py launcher you can simply launch your desired python version using-如果你有py启动器,你可以简单地使用 - 启动你想要的 python 版本 -

py -3.6-64

The above will launch the 64 bit version of python 3.6 (if installed).以上将启动 python 3.6 的 64 位版本(如果已安装)。

Now, you'll also want to use a virtual env and point pycharm to the venv.现在,您还需要使用虚拟环境并将 pycharm 指向 venv。 To make a venv , go to your project directory (preferably) and do-要制作一个venv , go 到您的项目目录(最好)并做-

> py -3.6-64 -m venv name_of_venv

This will make a venv named of name_of_venv in your project directory and the python version will be 3.6 64 bit.这将在您的项目目录中创建一个名为name_of_venv的 venv,并且 python 版本将为 3.6 64 位。

Now whenever you need to do any python commands, you no longer need to do py -version , you can just do python and even use pip - BUT before that, you need to activate the venv现在,每当您需要执行任何 python 命令时,您不再需要执行py -version ,您只需执行python甚至使用pip - 但在此之前,您需要venv

> & '.\name_of_venv\Scripts\Activate.ps1'

Now you can do normal python operations as long within this venv and it'll all target 3.6 64 bit (or any other version you choose to build the venv with).现在您可以在此 venv 中执行正常的 python 操作,并且它将全部针对 3.6 64 位(或您选择用于构建venv的任何其他版本)。

To deactivate (though you don't really have to) - you can just type deactivate in the terminal.要停用(尽管您实际上不必这样做) - 您只需在终端中输入deactivate即可。

Pycharm can be configured with this venv as simply as just pointing to it. Pycharm 可以用这个venv进行配置,就像指向它一样简单。 You simply have to go to Add python interpreter and choose Virtualenv Environment您只需 go添加 python 解释器并选择Virtualenv Environment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM