2017-03-28 58 views
14

我正在使用python 2.7 + virtualenv版本1.10.1用於運行myproject項目。由於一些其他項目的要求,我必須與其他版本的Python(Python 3.5)和Django 1.9一起工作。爲此,我在我的用戶目錄中安裝了python。此外,我已下載並將virtualenv(版本 - 15.1.0)安裝到我的用戶目錄中。 但每當我試圖創建虛擬ENV我提示以下錯誤:如何使用python3創建虛擬環境

python virtualenv/virtualenv.py myproject 

Using base prefix '/home/myuser/python3' 
New python executable in /home/mount/myuser/project_python3/myproject/bin/python 
ERROR: The executable /home/mount/myuser/project_python3/myproject/bin/python is not functioning 
ERROR: It thinks sys.prefix is '/home/myuser/python3' (should be '/home/mount/myuser/project_python3/myproject') 
ERROR: virtualenv is not compatible with this system or executable 

有誰能夠告訴我在做什麼錯這個

+1

您不清楚您在運行時使用的是哪種Python 'python virtualenv/virtualenv.py myproject'。 'python'在這裏是你的系統Python(2.7)還是你的本地Python(3.5)? – Chris

+1

您是否使用正確的點即pip3來安裝virtualenv版本 - 15.1.0? –

+0

我總是發現[這些文檔](http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/)很有幫助。 – blacksite

回答

28

在Python 3.6+中,不推薦使用pyvenv模塊。請使用以下單行代碼:

python3 -m venv <myenvname> 

這是recommended way來由Python社區創建虛擬環境。

5

Python中已經附帶了內置「 virtualenv「自3.3版以來稱爲venv。您不再需要爲Python 3.3+安裝或下載virtualenv腳本。

https://docs.python.org/3/library/venv.html

檢查您的安裝提供的pyvenv命令應該採取創建「的virtualenv」的照顧。參數類似於經典的virtualenv項目。

$ pyvenv --help 
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] 
      [--upgrade] [--without-pip] 
      ENV_DIR [ENV_DIR ...] 

Creates virtual Python environments in one or more target directories. 

positional arguments: 
    ENV_DIR    A directory to create the environment in. 

optional arguments: 
    -h, --help   show this help message and exit 
    --system-site-packages 
         Give the virtual environment access to the system 
         site-packages dir. 
    --symlinks   Try to use symlinks rather than copies, when symlinks 
         are not the default for the platform. 
    --copies    Try to use copies rather than symlinks, even when 
         symlinks are the default for the platform. 
    --clear    Delete the contents of the environment directory if it 
         already exists, before environment creation. 
    --upgrade    Upgrade the environment directory to use this version 
         of Python, assuming Python has been upgraded in-place. 
    --without-pip   Skips installing or upgrading pip in the virtual 
         environment (pip is bootstrapped by default) 

Once an environment has been created, you may wish to activate it, e.g. by 
sourcing an activate script in its bin directory. 
+4

儘管模塊仍然存在,但在Python 3.6及更高版本中不推薦使用此腳本。推薦使用的是:''python3 -m venv '' –

+0

@ user73657我正在使用python 3.5。但是,如果我運行pyenv命令找不到它的命令 – Anish

+0

@ user73657 pyenv在3.5.3 – Anish

-1

在virtualenv之上安裝virtualenvwrapper以簡化事情。 按照博客中簡單的步驟安裝:virtualenvwrapper

步驟來創建它:

  1. mkvirtualenv -p在/ usr/bin中/ python3
  2. 使用安裝軟件包 - PIP安裝程序包
  3. workon - 激活virtualenv,deactivate - 取消激活viirtualenv
+1

如果'virtualenv'本身不工作,如何在'virtualenv'附近添加一個包裝? – Chris

+0

這是一個重複的答案。這也是一個不正確的答案。 virtualenv已被棄用,這是一個關於virtualenv的封裝 –