简体   繁体   English

管理 Python 的多个版本

[英]Managing several versions of Python

Ok, I am relatively new to Python (more of a MATLAB / R /Stata user).好的,我对 Python 比较陌生(更多的是 MATLAB / R /Stata 用户)。 I previously installed Python on my computer from the Python website.我之前从 Python 网站在我的电脑上安装了 Python。 Everything was running smoothly until I had to install Pytorch too.在我不得不安装 Pytorch 之前,一切都运行顺利。 I tried installing it via pip to no avail, so I had to make a new installation of Python but this time with Anaconda.我尝试通过 pip 安装它无济于事,所以我不得不重新安装 Python 但这次是 Anaconda。

However, now I have a mess and I can not load Scypi on Anaconda and I can not load Pytorch in the regular Python I have.但是,现在我一团糟,无法在 Anaconda 上加载 Scypi,也无法在我拥有的常规 Python 中加载 Pytorch。 Having to run them separately is driving me insane.不得不单独运行它们让我发疯。 Is there a way that I can merge the two versions together or should I uninstall and stick to only one?有没有办法可以将两个版本合并在一起,还是应该卸载并只使用一个?

Have you tried pip3 install pytorch?您是否尝试过 pip3 install pytorch? Sometimes Python2 is the main version.有时 Python2 是主要版本。 To use python 3 pip instead you have to use pip3 install要使用 python 3 pip ,您必须使用 pip3 install

Besides the solution of virtual environments, if you are on Windows and have the py launcher installed, you could call the specified version of python using py -3 or py -2 or py -3.6 or py 3.9 as required.除了虚拟环境的解决方案,如果你在 Windows 并安装了py启动器,你可以根据需要使用py -3py -2py -3.6py 3.9调用指定版本的 python。 Or use the 'shebang' line in the python script for *nix.或者在 *nix 的 python 脚本中使用“shebang”行。

More about py-launcher and shebang here: https://www.python.org/dev/peps/pep-0397/更多关于 py-launcher 和 shebang 的信息: https://www.python.org/dev/peps/pep-0397/

Put this in environment.yml file把它放在 environment.yml 文件中

name: myenv
channels:
  - pytorch
  - anaconda
dependencies:
  - pytorch
  - torchvision
  - torchaudio
  - cpuonly
  - scipy

You can change the name so that it suits your taste.您可以更改名称以使其适合您的口味。
You should modify dependencies according to the install command provided by https://pytorch.org .您应该根据https://pytorch.org提供的安装命令修改依赖项。
And you can continue after running the following command.您可以在运行以下命令后继续。

conda activate [your environment name]

But as described in the conda documentation , if you want to use pip, you should install pip for your conda environment and then use it after using conda for as many packages as possible.但是如 conda 文档中所述,如果您想使用 pip,您应该为您的 conda 环境安装 pip,然后在将 conda 用于尽可能多的包后使用它。

This way, you don't need to uninstall your original python.这样,您无需卸载原来的 python。 And in regard to merging the two environment, I haven't heard of it and it seems not a good practice if it's possible.关于合并两个环境,我还没有听说过,如果可能的话,这似乎不是一个好习惯。

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

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