Virtualenv包安装和导入

时间:2014-11-15 00:46:09

标签: python virtualenv

我是Python的新手,但决定使用their tutorial试用Pyramid Web应用程序框架。我修改了一些步骤,以使用现代工具处理此问题的正确方法使其与最新版本保持同步。

作为上下文,我的Mac(Yosemite)上使用macport安装了virtualenv和我的python(版本3.4)。

这就是我所做的:

$ virtualenv swapenv
Using base prefix '/opt/local/Library/Frameworks/Python.framework/Versions/3.4'
New python executable in swapenv/bin/python3.4
Also creating executable in swapenv/bin/python
Installing setuptools, pip...done.
$ source swapenv/bin/activate
(swapenv)$ pip install pyramid
... (successful install of pyramid with some warnings) ...

到目前为止,这么好。现在我试着看看我是否可以验证安装,看看python找不到它。我敲了一个小时。然后考虑尝试python3

(swapenv)$ python
Python 3.4.2 (default, Oct 19 2014, 11:45:18) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyramid
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'pyramid'
>>> exit()
(swapenv)$ python3
Python 3.4.2 (default, Oct 19 2014, 11:45:18) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyramid
>>> 

因此,如果我使用python3则可行,但python则不然。我去核实这些安装:

(swapenv)rook-4:bin nateford$ pwd
/Users/nateford/experiments/pyramid/swapenv/bin
(swapenv)rook-4:bin nateford$ which python
/Users/nateford/experiments/pyramid/swapenv/bin/python
(swapenv)rook-4:bin nateford$ which python3
/Users/nateford/experiments/pyramid/swapenv/bin/python3

好的,所以指向不同的地方,对吗?或者不......?

(swapenv)rook-4:bin nateford$ cd /Users/nateford/experiments/pyramid/swapenv/bin
(swapenv)rook-4:bin nateford$ ls -la python*
lrwxr-xr-x  1 nateford  staff      9 Nov 14 16:25 python -> python3.4
lrwxr-xr-x  1 nateford  staff      9 Nov 14 16:25 python3 -> python3.4
-rwxr-xr-x  1 nateford  staff  13180 Nov 14 16:25 python3.4

为什么使用python的结果与使用python3的结果不同?有没有办法诊断差异?有没有办法纠正差异(我不打算使用python 2,所以希望所有内容都指向python3)。

更新:根据评论请求,python -m site命令

输出以下内容
(swapenv)rook-4:bin nateford$ python -m site
sys.path = [
    '/Users/nateford/experiments/pyramid/swapenv/bin',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/webapp-0.1-py3.4.egg',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages',
]
USER_BASE: '/Users/nateford/Library/Python/3.4' (doesn't exist)
USER_SITE: '/Users/nateford/Library/Python/3.4/lib/python/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
(swapenv)rook-4:bin nateford$ python3 -m site
sys.path = [
    '/Users/nateford/experiments/pyramid/swapenv/bin',
    '/Users/nateford/experiments/pyramid/swapenv/lib/python34.zip',
    '/Users/nateford/experiments/pyramid/swapenv/lib/python3.4',
    '/Users/nateford/experiments/pyramid/swapenv/lib/python3.4/plat-darwin',
    '/Users/nateford/experiments/pyramid/swapenv/lib/python3.4/lib-dynload',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin',
    '/Users/nateford/experiments/pyramid/swapenv/lib/python3.4/site-packages',
]
USER_BASE: '/Users/nateford/.local' (doesn't exist)
USER_SITE: '/Users/nateford/.local/lib/python3.4/site-packages' (doesn't exist)
ENABLE_USER_SITE: False

1 个答案:

答案 0 :(得分:0)

似乎您的pythonpython3指向创建虚拟环境很重要。解决此问题的第一步是确保MacPorts选择了正确的Python版本:

sudo port select --set python python34

然后正常创建虚拟环境。这可以确保虚拟环境创建的pippython别名和bin链接的python都指向同一方向。