如何在Ubuntu中使用Python3.3创建virtualenv?


46

我正在运行精确的穿山甲amd64。我从ppa:fkrull / deadsnakes安装了Python 3.3。

现在,我想在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

我确实安装了package python3-setuptools,它安装了easy_install的Py3版本。

编辑2:

这是详细输出,没有显式传递--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'

为了清楚起见,您可以尝试以下命令(不包括distribute):virtualenv --no-site-packages -p /usr/bin/python3.3 ~/.virtualenvs/pywork3吗?
don.joey 2013年

我也尝试过,--setuptools结果完全一样(包括分发的安装)。
andreas-h 2013年

在14.04中,所有工作都轻松进行。
Antti Haapala 2014年

Answers:


25
  1. 备份站点程序包。

    cp -r /usr/local/lib/python2.7/site-packages/ /tmp/site-packages

  2. 截断该目录

    rm -rf /usr/local/lib/python2.7/site-packages/

  3. 现在尝试相同

    virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3

  4. 要安装而没有pip

    virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3 --no-pip

编辑:看起来virtualenv(1.7.1.2)和python3.3的版本不兼容。所以,请尝试这个

  1. 使用卸载python-virtualenv sudo apt-get remove python-virtualenv

  2. wget http://peak.telecommunity.com/dist/ez_setup.py; sudo python ez_setup.py 这将安装最新的easy_install。

  3. sudo easy_install pip

  4. sudo pip install virtualenv

  5. virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3


有趣的是,我现在要进一步走,但仍然不能一直走下去。请参阅我最新的问题
andreas-h 2013年

我正在查看virtualenv代码。如果您可以以更高的详细度执行同一命令并向我们展示完整的输出,将很有帮助。 virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3 --verbose
thefourtheye 2013年

知道了,请参阅我的最新问题。
andreas-h 2013年

已经分析了两个多小时。看起来distribute不会在bin目录中安装easy_install脚本。我们可以稍后手动安装它们。因此,仅安装virtualenv,尝试不使用pip进行安装,正如我在答案中
所做的

1
我没有手动安装virtualenv,而是从packages.ubuntu.com/raring/all/python-virtualenv/download下载了Ubuntu Raring的.dev包,并通过安装了它dpkg -i。似乎现在可以工作。非常感谢!
andreas-h

58

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,它似乎可以做很多事情,但是从本质上讲,您最终会easy_install进入./my_project/venv/bin目录。
然后可以使用它进行安装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更加内联地完成虚拟环境的设置


2
不错-我不知道这个...
andreas-h 13-10-10

1
实际上,这似乎不是一个“真实的” virtualenv。激活后既没有pip也没有easy_install。
andreas-h 2013年

@ andreas-h您需要自己安装那些。那并不是说它不是“真实的”。
克里斯(Chris)

1
@安德烈亚斯-H此外,它的设计,让你可以创建一个脚本来添加任何你想要的自己..看到docs.python.org/3/library/...
克里斯-

如果终端在安装pip时大喊错误的权限,请检查您是否使用了easy_install的正确二进制文件。您可以通过在virtualenv中显式选择二进制文件./venv/bin/easy_install pip
OrangeTux 2013年


8

您这样做太复杂了。如果包含在中python3,只需使用以下命令创建它:

python3 -m venv

您甚至可以创建别名

alias virtualenv3='python3 -m venv'

根据机器,您可能需要指定解释器:

alias virtualenv3='python3 -m venv -p python3'

python3-venv至少在Ubuntu 14.04上也需要该软件包。
gioele 2016年

对我来说(也在14.04上),该包称为python3.4-venv
tobek

2

对我有用的步骤:在MAC OSX 10.9.5上

  1. 手动下载并安装Python-3.4.3

    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
  2. 创建一个virtualenv。

    $P3PATH/Python-3.4.3/bin/pyvenv py3env
    source py3env/bin/activate

注意:
virtualenv -p <path to python3> py3env没有用:

Error due to zlib

这在Debian 7上也可以正常使用。只是另一个P3PATH
rubo77

对于基于debian的发行版in-a-container用户,请确保安装libssl-devzlib1g-dev使用_ssl.so zlib.so编译python
Nehal J Wani

1
On Ubuntu 14.04 

sudo apt-get install python3-pip

sudo pip install virtualenv

virtualenv-3.4 --no-site-packages venv
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.