【问题标题】:Should I use pip or pip3 to install python3 packages inside of a virtual environment?我应该使用 pip 还是 pip3 在虚拟环境中安装 python3 包?
【发布时间】:2016-10-22 08:00:22
【问题描述】:

我的 Linux 机器上有 python 2(默认)和 3。我创建了一个虚拟环境来试用pandas 和python3。所以,我跑了:

virtualenv -p python3 pandas_env

如果我在这个环境中输入python,它会进入标准的python shell,默认版本是python 3

(panda_env)$ python
Python 3.4.3 (default, Sep 14 2016, 12:36:27) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

为了安装 ipython,我做了:

(panda_env)$ pip install ipython

现在,运行 ipython,它默认为 python2 而不是 3

(panda_env)$ ipython
WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
Type "copyright", "credits" or "license" for more information.

IPython 2.3.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

WARNING 说要在 virtualenv 中安装 ipython。我还以为是在环境里面安装了ipython,为什么默认使用python2.x呢?

如何在 virtualenv 中正确安装 ipython,如何让 ipython 使用 3.x 而不是 2.x?

另外,我应该使用 pip 还是 pip3 在 vi​​rtualenv 中安装软件包?

【问题讨论】:

  • python $(which ipython)?我想你可能没有在 virtualenv 中成功安装 ipython。
  • 非常感谢,这行得通,.. 但我应该在未来的这个环境中pip install <package> 或pip3 install <package> .. 例如,我创建了这个环境来锻炼熊猫。要安装 pandas 及其依赖的模块,我应该使用 pip install pandas 还是 pip3 install pandas
  • 是的,我没有正确安装它,当我在环境内的外壳上执行ipython 时,解释器就是这么说的。你能帮忙看看如何正确安装它们吗
  • 尝试which pip 和which pip3 看看是否每个都在virtualenv 中。我认为他们都应该这样做,在这种情况下他们应该做同样的事情。
  • 对于(env)$ which pip,我得到~/panda_env/bin/pip,对于(env)$ which pip3,我得到~/panda_env/bin/pip3

标签: pip ipython virtualenv python-3.4


【解决方案1】:

注意,对于 python > 3.3,建议您使用 pyvenv 命令启动虚拟环境。从 python > 3.6 开始,这已被弃用,取而代之的是 python3 -m venv <environment name>(通过 https://docs.python.org/3/library/venv.html)

在回答您的问题时,基于此检查,我认为这并不重要:

# start enviroment
$ python3 -m venv test_env; source test_env/bin/activate

# check python version 
(test_env) $ python --version
Python 3.6.0

# check pip and pip3
(test_env) $ which pip
~/Documents/test_env/bin/pip
(test_env) $ which pip3
~/Documents/test_env/bin/pip3

# The pip commands are different, but if we check their versions they are the same and both appear to be using python3.
(test_env) $ pip -V
pip 9.0.1 from ~/test_env/lib/python3.6/site-packages (python 3.6)
(test_env) $ pip3 -V
pip 9.0.1 from ~/test_env/lib/python3.6/site-packages (python 3.6)

# In fact the binaries for pip and pip3 within the environment are identical as you can see with this.
(test_env) $ diff ~/Documents/test_env/bin/pip ~/Documents/test_env/bin/pip3

# Checking with ipython
(test_env) $ pip install ipython
(test_env) $ ipython 
Python 3.6.0 (default, Dec 24 2016, 08:01:42)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.

【讨论】:

    【解决方案2】:

    使用虚拟环境最简单的方法是下载 anaconda 并使用 conda 版本控制系统:

    • 所有版本的 python 的工作方式完全相同
    • Python 被视为环境中的另一个包
    • 与环境相关的所有内容都存储在一个文件夹中
    • 如果某些内容停止工作,您可以查看修订历史记录并回滚到以前的环境版本

    【讨论】:

    • 这并不能真正回答操作员的问题。此外,Anaconda 可能会满足您的需求,但它可能会成为其他应用程序的障碍。
    • 我从未遇到过可能会造成障碍的情况。能举个例子吗?
    • Web 开发,我也让它干扰自制程序,使 python 可移植 - 只需发送带有 requirements.txt 文件的代码然后运行 ​​pip install -r requirements.txt
    • 为什么这是“最简单的”?是什么让它比任何其他方法更容易?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2017-06-15
    • 1970-01-01
    • 2021-10-24
    • 2017-05-14
    • 2019-06-09
    • 2020-08-23
    相关资源
    最近更新 更多