问题描述
我正在运行Precise Pangolin amd64。我从ppa安装了Python 3.3:fkrull /deadsnakes。
现在我想在virtualenv中实际使用这个新的Python版本。但我怎么能这样做?我收到以下错误:
$ virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3
Running virtualenv with interpreter /usr/bin/python3.3
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3
Also creating executable in /home2/USERNAME/.virtualenvs/pywork3/bin/python
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/site.py", line 73, in <module>
__boot()
File "/usr/local/lib/python2.7/site-packages/site.py", line 2, in __boot
import sys, imp, os, os.path
ImportError: No module named 'imp'
ERROR: The executable /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3 is not functioning
ERROR: It thinks sys.prefix is '/home2/USERNAME/.virtualenvs' (should be '/home2/USERNAME/.virtualenvs/pywork3')
ERROR: virtualenv is not compatible with this system or executable
如果相反,我明确使用python3.3
来调用virtualenv,我收到此错误:
$ python3.3 /usr/bin/virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3
Traceback (most recent call last):
File "/usr/bin/virtualenv", line 2, in <module>
import virtualenv
ImportError: No module named 'virtualenv'
我被卡住了。任何帮助是极大的赞赏!
编辑:按照@thefourtheye的建议,我清除了我当地的site-packages
目录(实际上只删除了site.py
)。现在我更进了一步,但是virtualenv抱怨缺少easy_install
:
Error [Errno 2] No such file or directory:
'/home2/USERNAME/.virtualenvs/pywork3/bin/easy_install' while executing command
/home2/USERNAME/.virt...rk3/bin/easy_install /usr/share/python-virtualenv/pip-1.1.tar.gz
我安装了python3-setuptools
软件包,它安装了Py3版本的easy_install。
EDIT2:
这是详细的输出,没有显式传递--distribute
和--no-site-packages
,因为这两个开关是我的virtualenv的默认行为:
$ virtualenv --verbose -p /usr/bin/python3.3 ~/.virtualenvs/pywork3
Running virtualenv with interpreter /usr/bin/python3.3
Creating /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3
Symlinking Python bootstrap modules
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/config-3.3m
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/lib-dynload
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/os.py
Ignoring built-in bootstrap module: posix
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/posixpath.py
Cannot import bootstrap module: nt
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/ntpath.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/genericpath.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/fnmatch.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/locale.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/encodings
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/codecs.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/stat.py
Cannot import bootstrap module: UserDict
Cannot import bootstrap module: copy_reg
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/types.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/re.py
Cannot import bootstrap module: sre
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/sre_parse.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/sre_constants.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/sre_compile.py
Ignoring built-in bootstrap module: zlib
Cannot import bootstrap module: _abcoll
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/warnings.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/linecache.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/abc.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/io.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/_weakrefset.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/copyreg.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/tempfile.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/random.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/__future__.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/collections
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/keyword.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/tarfile.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/shutil.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/struct.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/copy.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/base64.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/bisect.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/_dummy_thread.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/hashlib.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/heapq.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/hmac.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/reprlib.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/rlcompleter.py
Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/weakref.py
Creating /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/site-packages
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/site.py
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/orig-prefix.txt
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/no-global-site-packages.txt
Creating parent directories for /home2/USERNAME/.virtualenvs/pywork3/include
Symlinking /home2/USERNAME/.virtualenvs/pywork3/include/python3.3m
Creating /home2/USERNAME/.virtualenvs/pywork3/bin
New python executable in /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3
Changed mode of /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3 to 0o755
Also creating executable in /home2/USERNAME/.virtualenvs/pywork3/bin/python
Changed mode of /home2/USERNAME/.virtualenvs/pywork3/bin/python to 0o755
Testing executable with /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3 -c "
import sys
prefix = sys.prefix
if sys.version_info[0] == 3:
prefix = prefix.encode('utf8')
if hasattr(sys.stdout, 'detach'):
sys.stdout = sys.stdout.detach()
elif hasattr(sys.stdout, 'buffer'):
sys.stdout = sys.stdout.buffer
sys.stdout.write(prefix)
"
Got sys.prefix result: '/home2/USERNAME/.virtualenvs/pywork3'
Creating /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/distutils
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/distutils/__init__.py
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/distutils/distutils.cfg
Using existing distribute egg: /usr/share/python-virtualenv/distribute-0.6.24.tar.gz
Installing distribute..............................................................................................................................................................................................................................................................................................................................................................................................................done.
Installing existing pip-1.1.tar.gz distribution: /usr/share/python-virtualenv/pip-1.1.tar.gz
Installing pip...
Error [Errno 2] No such file or directory: '/home2/USERNAME/.virtualenvs/pywork3/bin/easy_install' while executing command /home2/USERNAME/.virt...rk3/bin/easy_install /usr/share/python-virtualenv/pip-1.1.tar.gz
...Installing pip...done.
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 2283, in <module>
main()
File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 938, in main
never_download=options.never_download)
File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 1054, in create_environment
install_pip(py_executable, search_dirs=search_dirs, never_download=never_download)
File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 643, in install_pip
filter_stdout=_filter_setup)
File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 976, in call_subprocess
cwd=cwd, env=env)
File "/usr/lib/python3.3/subprocess.py", line 818, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.3/subprocess.py", line 1416, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: '/home2/USERNAME/.virtualenvs/pywork3/bin/easy_install'
最佳解决方法
-
备份site-packages。
cp -r /usr/local/lib/python2.7/site-packages/ /tmp/site-packages
-
截断该目录
rm -rf /usr/local/lib/python2.7/site-packages/
-
现在尝试相同的
virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3
-
无pip
virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3 --no-pip
即可安装
编辑:看起来virtualenv(1.7.1.2)和python3.3的版本不兼容。所以,请试试这个
-
使用
sudo apt-get remove python-virtualenv
卸载python-virtualenv -
wget http://peak.telecommunity.com/dist/ez_setup.py; sudo python ez_setup.py
这将安装最新的easy_install。 -
sudo easy_install pip
-
sudo pip install virtualenv
-
virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3
次佳解决方法
Python 3.3有venv 内置。
http://docs.python.org/3/library/venv.html#module-venv
简单地跑
pyvenv-3.3 /path/to/environment
然后激活它
source /path/to/environment/bin/activate
内置版本的virtualenv比您可能习惯的更灵活。例如,您可以扩展EnvBuilder以执行任何您想要的操作。您可以从下面的链接复制EnvBuilder的示例实现并使用它:http://docs.python.org/3/library/venv.html#an-example-of-extending-envbuilder
上面的脚本可能会完成我们期望从virtualenv中获得的大部分内容。因此,如果你只需要一个带有easy_install和pip的virtualenv,你应该很高兴。
请参阅下面的@MarkOfSine编辑,以了解如果您仍然感到困惑,如何开始运行。
要添加到上面,并按照文档:例如,执行后:pyvenv-3.3 /path/to/my_project/venv
您可以运行distribute_setup.py
,这似乎做了各种各样的事情,但基本上你最终在./my_project/venv/bin
目录中使用easy_install
。然后,这可以用于安装pip
等。
它没有说你应该从哪里获得distribute_setup.py
,所以我从以下网址下载:
http://python-distribute.org/distribute_setup.py
并使用激活的环境:
cd /path/to/my_project
source venv/bin/activate
跑了:
python distribute_setup.py
和
easy_install pip
然后使用python 2.x上的virtualenv完成了虚拟环境的设置
第三种解决方法
它比看起来容易:
virtualenv -p /usr/bin/python3 yourenv
source yourenv/bin/activate
pip install package-name
真的有效:)
第四种方法
你这样做太复杂了。如果它包含在python3
中,只需创建它:
python3 -m venv
如果您愿意,甚至可以创建别名
alias virtualenv3='python3 -m venv'
根据计算机的不同,您可能需要指定解释器:
alias virtualenv3='python3 -m venv -p python3'
第五种方法
对我有用的步骤:在MAC OSX 10.9.5上
-
下载并安装Python-3.4.3 manually。
P3PATH=/Users/$USER/Python3 mkdir -p $P3PATH cd $P3PATH tar -zxvf Python-3.4.3.tgz ./configure --prefix=$P3PATH/Python-3.4.3 make; make install
-
创建一个virtualenv。
$P3PATH/Python-3.4.3/bin/pyvenv py3env source py3env/bin/activate
注意:virtualenv -p <path to python3> py3env
不起作用:
Error due to zlib