简体   繁体   English

如何在计算机上管理不同的python版本

[英]how to manage different python versions on a computer

I recently moved my os from debian7 to archlinux. 我最近将操作系统从debian7移到了archlinux。 On debian 7, the default python version is python2.7 but on archlinux the default is python3.4. 在debian 7上,默认的python版本是python2.7,但是在archlinux上,默认的是python3.4。

I once wrote a spider program using beautifulsoup4 on my debian7 but now I want to run this program on archlinux. 我曾经在debian7上使用beautifulsoup4编写了一个Spider程序,但现在我想在archlinux上运行该程序。

I use sudo pacman -S python-pip to install pip and then use sudo pip install beautifulsoup4 . 我使用sudo pacman -S python-pip安装pip,然后使用sudo pip install beautifulsoup4 But this time this package goes into /usr/lib/python3.4/site-packages/ . 但是这次,这个包进入/usr/lib/python3.4/site-packages/

I know I can download bs4 to my spider's directory and run it with python2, but I just want to know how do you install packages using pip since you have 2 python version installed. 我知道我可以将bs4下载到我的Spider目录中并使用python2运行它,但是我只想知道如何使用pip安装软件包,因为您已经安装了2个python版本。 And btw if I change the default python link to python2*, will it break my system? 顺便说一句,如果我将默认的python链接更改为python2 *,它将破坏我的系统吗?

Try python2-pip and pip2 or pip2.7 . 尝试使用python2-pippip2pip2.7

In arch linux all of the python 3 packages have no version number and all the python 2 packages have a 2 . 在arch linux中,所有python 3软件包都没有版本号,所有python 2软件包都有2 The commands are the same way. 命令是相同的方式。

To select what version a script uses use the version number in the hashbang. 要选择脚本使用的版本,请使用井号中的版本号。

#! /usr/bin/env python2

or 要么

#! /usr/bin/env python3

I'm not sure if debian has a python2 link but they can easily be created on systems where /usr/bin/python is python2.7 . 我不确定debian是否具有python2链接,但是可以在/usr/bin/pythonpython2.7系统上轻松创建它们。

sudo ln -s /usr/bin/python /usr/bin/python{2,2.7}

"on archlinux the default is python3.4" - wow, this is ambitious. “在archlinux上,默认值为python3.4” -哇,这很雄心勃勃。

The archlinux wiki on Python provides a lot of tips. Python上archlinux Wiki提供了很多技巧。 You should install python2-pip to make sure that pip is installed for the right version. 您应该安装python2-pip ,以确保为正确的版本安装了pip。

Archlinux usually provides packages prefixed with python- for Python 3.x and python2- for Python 2.x. 对于python2- 3.x,Archlinux通常提供带有python-和Python 2.x的python2-前缀的软件包。 You can also use virtualenv to manage local Python environments, which means you can have a Python2 project and a Python3 project without conflicts between them. 您还可以使用virtualenv来管理本地Python环境,这意味着您可以拥有一个Python2项目和一个Python3项目,而它们之间没有冲突。

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

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