我应该使用pip还是pip3在虚拟环境中安装python3软件包?

时间:2016-10-22 08:00:22

标签: pip ipython virtualenv python-3.4

我的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?

另外,我应该使用pippip3在virtualenv中安装软件包吗?

2 个答案:

答案 0 :(得分:0)

注意,对于python&gt; 3.3建议您使用pyvenv命令启动虚拟环境。从python&gt;开始,这已经被弃用了。 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)

# 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.

答案 1 :(得分:-1)

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

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