在virtualenv中使用Python 3

本文翻譯自:Using Python 3 in virtualenv

Using virtualenv , I run my projects with the default version of Python (2.7). 使用virtualenv ,我使用默認版本的Python(2.7)運行項目。 On one project, I need to use Python 3.4. 在一個項目中,我需要使用Python 3.4。

I used brew install python3 to install it on my Mac. 我使用brew install python3將其安裝在Mac上。 Now, how do I create a virtualenv that uses the new version? 現在,如何創建使用新版本的virtualenv?

eg sudo virtualenv envPython3 例如sudo virtualenv envPython3

If I try: 如果我嘗試:

virtualenv -p python3 test

I get: 我得到:

Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.4.0_1/Frameworks/Python.framework/Versions/3.4'
New python executable in test/bin/python3.4
Also creating executable in test/bin/python
Failed to import the site module
Traceback (most recent call last):
  File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/site.py", line 67, in <module>
    import os
  File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/os.py", line 634, in <module>
    from _collections_abc import MutableMapping
ImportError: No module named '_collections_abc'
ERROR: The executable test/bin/python3.4 is not functioning
ERROR: It thinks sys.prefix is '/Users/user/Documents/workspace/test' (should be '/Users/user/Documents/workspace/test/test')
ERROR: virtualenv is not compatible with this system or executable

#1樓

參考:https://stackoom.com/question/1c2Zt/在virtualenv中使用Python


#2樓

simply run 只需運行

virtualenv -p python3 envname

Update after OP's edit: OP編輯後更新:

There was a bug in the OP's version of virtualenv, as described here . 有沒有在OP的版本virtualenv中的一個bug,如所描述這裏 The problem was fixed by running: 該問題已通過運行解決:

pip install --upgrade virtualenv

#3樓

Python 3 has a built-in support for virtual environments - venv . Python 3具有對虛擬環境venv的內置支持。 It might be better to use that instead. 最好改用它。 Referring to the docs: 參考文檔:

Creation of virtual environments is done by executing the pyvenv script: 通過執行pyvenv腳本來創建虛擬環境:

pyvenv /path/to/new/virtual/environment

Update for Python 3.6 and newer: 適用於Python 3.6及更高版本的更新:

As pawciobiel correctly comments , pyvenv is deprecated as of Python 3.6 and the new way is: pawciobiel正確註釋時 ,從Python 3.6開始不推薦使用 pyvenv ,新方法是:

python3 -m venv /path/to/new/virtual/environment

#4樓

In addition to the other answers, I recommend checking what instance of virtualenv you are executing: 除了其他答案,我建議檢查您正在執行哪個virtualenv實例:

which virtualenv

If this turns up something in /usr/local/bin, then it is possible - even likely - that you installed virtualenv (possibly using an instance of easy_tools or pip) without using your system's package manager (brew in OP's case). 如果在/ usr / local / bin中出現問題,則可能甚至可能安裝了virtualenv(可能使用easy_tools或pip實例)而沒有使用系統的程序包管理器(在OP中爲棕色)。 This was my problem. 這是我的問題。

Years ago - when I was even more ignorant - I had installed virtualenv and it was masking my system's package-provided virtualenv. 多年前-當我更加無知的時候-我安裝了virtualenv,它掩蓋了我係統的軟件包提供的virtualenv。

After removing this old, broken virtualenv, my problems went away. 刪除了這個破舊的virtualenv之後,我的問題就消失了。


#5樓

I'v tried pyenv and it's very handy for switching python versions (global, local in folder or in the virtualenv): 我嘗試過pyenv ,它對於切換python版本(全局,文件夾或virtualenv中的本地)非常方便:

brew install pyenv

then install Python version you want: 然後安裝所需的Python版本:

pyenv install 3.5.0

and simply create virtualenv with path to needed interpreter version: 並只需創建virtualenv幷包含所需解釋器版本的路徑即可:

virtualenv -p /Users/johnny/.pyenv/versions/3.5.0/bin/python3.5 myenv

That's it, check the version: 就是這樣,檢查版本:

. ./myenv/bin/activate && python -V

There are also plugin for pyenv pyenv-virtualenv but it didn't work for me somehow. 也有pyenv的插件pyenv-virtualenv,但是它對我不起作用。


#6樓

對我有用

virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章