Linux 在 Ubuntu 上安装 python 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19034959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:56:31  来源:igfitidea点击:

Installing python modules on Ubuntu

pythonlinuxubuntupython-3.xpackage

提问by Infamouslyuseless

I need to install some modules for python on Ubuntu Linux 12.04. I want pygame and livewires but I'm not sure how to install them.

我需要在 Ubuntu Linux 12.04 上为 python 安装一些模块。我想要 pygame 和 livewires,但我不知道如何安装它们。

I have the py file for livewires, which has been specially edited (from a book I'm reading) and I want to install it but I'm not sure how to, I also want to install pygame.

我有 livewires 的 py 文件,该文件经过特别编辑(来自我正在阅读的一本书),我想安装它,但我不确定如何安装,我也想安装 pygame。

回答by Alvaro

You can use several approaches:

您可以使用多种方法:

1 - Download the package by yourself. This is what I use the most. If the package follows the specifications, you should be able to install it by moving to its uncompressed folder and typing in the console:

1 - 自行下载软件包。这是我用得最多的。如果包符合规范,您应该能够通过移动到其未压缩文件夹并在控制台中键入来安装它:

python setup.py build
python setup.py install

2 - Use pip. Pip is pretty straightforward. In the console, you have to type:

2 - 使用点子。Pip 非常简单。在控制台中,您必须键入:

pip install package_name

You can obtain pip here https://pypi.python.org/pypi/pipand install it with method 1

您可以在此处获取 pip https://pypi.python.org/pypi/pip并使用方法 1 安装它

One thing to note: if you aren't using a virtualenv, you'll have to add sudo before those commands (not recommended)

需要注意的一件事:如果您不使用 virtualenv,则必须在这些命令之前添加 sudo (不推荐)

回答by Fred Foo

There are two nice ways to install Python packages on Ubuntu (and similar Linux systems):

在 Ubuntu(和类似的 Linux 系统)上安装 Python 包有两种不错的方法:

sudo apt-get install python-pygame

to use the Debian/Ubuntu package manager APT. This only works for packages that are shipped by Ubuntu, unless you change the APT configuration, and in particular there seems to be no PyGame package for Python 3.

使用 Debian/Ubuntu 包管理器 APT。这仅适用于 Ubuntu 提供的软件包,除非您更改 APT 配置,特别是似乎没有适用于 Python 3 的 PyGame 软件包。

The other option is to use PIP, the Python package manager:

另一种选择是使用 PIP,Python 包管理器:

sudo apt-get install python3-pip

to install it, then

安装它,然后

sudo pip3 install pygame

to fetch the PyGame package from PyPIand install it for Python 3. PIP has some limitations compared to APT, but it does always fetch the latest version of a package instead of the one that the Ubuntu packagers have chosen to ship.

PyPI获取 PyGame 包并为 Python 3 安装它。与 APT 相比,PIP 有一些限制,但它总是获取最新版本的包,而不是 Ubuntu 打包者选择发布的包。

EDIT: to repeat what I said in the comment, pip3isn't in Ubuntu 12.04 yet. It can still be installed with

编辑:重复我在评论中所说的,pip3还没有出现在 Ubuntu 12.04 中。它仍然可以安装

sudo apt-get install python3-setuptools
sudo easy_install3 pip
sudo apt-get purge python-pip

After this, pipis the Python 3 version of PIP, instead of pip3. The last command is just for safety; there might be a Python 2 PIP installed as /usr/bin/pip.

在此之后,pip是 PIP 的 Python 3 版本,而不是pip3. 最后一条命令只是为了安全;可能有一个 Python 2 PIP 安装为/usr/bin/pip.

回答by user1525721

Try to install pip.

尝试安装pip。

apt-get install python-pip
pip install pygame

回答by Foo Bar User

 curl -O http://python-distribute.org/distribute_setup.py
 sudo python distribute_setup.py
 sudo easy_install pygame

Differences between distribute, distutils, setuptools and distutils2

分发、distutils、setuptools 和 distutils2 之间的差异

回答by Roozbeh Bakhshi

It depends on the Ubuntu version and the IDE you are using. Ubuntu 15 and older come with Python 2.7 and Ubuntu 16.04 comes with both Python 2.7 and 3.5. Now based on the IDE you are using there are several ways to do this. Let`s say you only installed Spyder from Ubuntu app store or installed Jupyter. In other words you do not have a distribution like Anaconda or Enthought which install their own Python versions. This is important to pay attention to because once you are trying to install a package/library, you need to know which Python it is being installed to.

这取决于您使用的 Ubuntu 版本和 IDE。Ubuntu 15 及更早版本带有 Python 2.7,Ubuntu 16.04 带有 Python 2.7 和 3.5。现在基于您使用的 IDE,有几种方法可以做到这一点。假设您仅从 Ubuntu 应用商店安装了 Spyder 或安装了 Jupyter。换句话说,您没有像 Anaconda 或 Enthought 这样的发行版,它们安装了自己的 Python 版本。这一点很重要,因为一旦你尝试安装一个包/库,你就需要知道它被安装到哪个 Python 上。

Now assuming you just have an IDE that is connected to Ubuntu`s default Python versions, you can use the terminal to install your packages:

现在假设您只有一个连接到 Ubuntu 的默认 Python 版本的 IDE,您可以使用终端来安装您的软件包:

For python 2.7 use

对于 python 2.7 使用

pip install libraryname

For python 3.5 use

对于 python 3.5 使用

pip3 install libraryname

Sometimes, for reasons that I don`t know, during the package installation process, Linux blocks access to the Python so try these as well:

有时,出于我不知道的原因,在软件包安装过程中,Linux 会阻止对 Python 的访问,因此也可以尝试以下方法:

sudo apt install python-libraryname

and for Python 3.5

和 Python 3.5

sudo apt install python3-libraryname

These have helped me to install all the libraries that I need.

这些帮助我安装了我需要的所有库。

Now, if you are using a distribution like Aanaconda or Enthought, there is a good chance that the libraries that you are installing are not going to be added to the libraries that those distributions use. In order to install the libraries for these distributions, once you run the distribution, go to the ipython console and write

现在,如果您使用的是 Aanaconda 或 Enthought 之类的发行版,则很有可能您正在安装的库不会添加到这些发行版使用的库中。为了安装这些发行版的库,运行发行版后,转到 ipython 控制台并编写

!pip install libraryname

In case of Enthought, it has it`s own Package Manager where it has most of the libraries you need and you can install them there without using pip or anything else.

在 Enthought 的情况下,它有自己的包管理器,其中包含您需要的大部分库,您可以将它们安装在那里,而无需使用 pip 或其他任何东西。