在Mac OSX上的虚拟环境中安装Python 3的pip?

4

我可以在虚拟环境中的Mac上使用pip installimport安装几乎任何包,具体步骤如下:

设置虚拟环境:

Last login: Mon Oct  3 18:47:06 on ttys000
me-MacBook-Pro-3:~ me$ cd /Users/me/Desktop/
me-MacBook-Pro-3:Desktop me$ virtualenv env
New python executable in /Users/me/Desktop/env/bin/python
Installing setuptools, pip, wheel...done.
me-MacBook-Pro-3:Desktop me$ source env/bin/activate

让我们使用pip install安装pandas:

(env) me-MacBook-Pro-3:Desktop me$ pip install pandas
Collecting pandas
  Using cached pandas-0.19.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting pytz>=2011k (from pandas)
  Using cached pytz-2016.7-py2.py3-none-any.whl
Collecting python-dateutil (from pandas)
  Using cached python_dateutil-2.5.3-py2.py3-none-any.whl
Collecting numpy>=1.7.0 (from pandas)
  Using cached numpy-1.11.1-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting six>=1.5 (from python-dateutil->pandas)
  Using cached six-1.10.0-py2.py3-none-any.whl
Installing collected packages: pytz, six, python-dateutil, numpy, pandas
Successfully installed numpy-1.11.1 pandas-0.19.0 python-dateutil-2.5.3 pytz-2016.7 six-1.10.0

太好了!现在,让我们看看它在Python 2.7中是否可行:

(env) me-MacBook-Pro-3:Desktop me$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> exit()

pandas已加载2.7版,现在让我们尝试3.5版:

(env) me-MacBook-Pro-3:Desktop me$ python3
Python 3.5.0a4 (v3.5.0a4:413e0e0004f4, Apr 19 2015, 14:19:25) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'pandas'
>>> 

:(

我运行的是OSX El Capitan 10.11.6操作系统。如何在虚拟环境中导入非内置模块?我真的更愿意使用Python 3...


https://github.com/yyuu/pyenv 让我无论如何都能做到这一点 - 也许对你有用? - Ismail Moghul
1个回答

8
尝试使用virtualenv --python=$(which python3) env来创建虚拟环境。 默认情况下,创建虚拟环境时会使用安装它的Python二进制文件。因此,如果您在已安装Python2.7的系统上执行了pip install virtualenv,则virtualenv将默认使用Python2.7。您需要为不同的Python版本创建单独的虚拟环境。

当我运行它时,我得到了“可执行文件 /usr/bin/python3(来自--python=/usr/bin/python3)不存在”的错误提示。 - blacksite
当你运行 which python3 时,它会返回什么? - edrw
你可以使用 virtualenv --python=$(which python3) env 命令来创建一个高级的虚拟环境。 - edrw
我正在运行的是/Users/me/Desktop/env/bin/python。使用您的第二条评论起作用了,谢谢! - blacksite

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接