python之virtualenv的簡單使用

來源:互聯網
上載者:User

標籤:djang   document   span   bash   height   python   版本   虛擬環境   class   

什麼是virtualenv?

virtualenv可以建立獨立Python開發環境,比如當前的全域開發環境是python3.6,現在我們有一個項目需要使用django1.3,另一個項目需要使用django1.9,這個時候就可以使用virtualenv建立各自的python開發環境了。


virtualenv的優點
  • 使不同的應用開發環境獨立

  • 環境升級不影響其他的應用,也不會影響全域的python開發環境

  • 它可以防止系統中出現包管理混亂和版本的衝突


安裝和建立虛擬環境
cmd下輸入:前提是你的python安裝路徑已經被添加到環境變數中C:\>pip install virtualenv

建立虛擬環境:

C:\>virtualenv testenvUsing base prefix ‘c:\\python36‘New python executable in C:\testenv\Scripts\python.exe  //預設安裝在目前的目錄下Installing setuptools, pip, wheel...done.

可以在目前的目錄下看一下:


啟用和關閉當前的虛擬環境
C:\testenv\Scripts>activate           //啟用(testenv) C:\testenv\Scripts>        //注意終端發生了變化(testenv) C:\testenv\Scripts>deactivate         //關閉當前虛擬環境C:\testenv\Scripts>


列出當前的虛擬環境都安裝了哪些包
(testenv) c:\testenv\Scripts>pip3 listpip (9.0.1)setuptools (37.0.0)wheel (0.30.0)

現在你就可以在你建立的虛擬環境下安裝需要使用到的包了


virtualenvwrapper

它是virtualenv的擴充包,還記得上面嗎?virtualenv需要我們去啟用也就是active一下虛擬環境。那我們來看看virtualenvwrapper的優點所在。

c:\>pip install virtualenvwrapper-win   //安裝c:\>mkvirtualenv testenv2  //建立虛擬環境 C:\Users\liubin\Envs is not a directory, creatingUsing base prefix ‘c:\\python36‘New python executable in C:\Users\liubin\Envs\testenv2\Scripts\python.exe   //virtualenvwrapper預設會把虛擬環境放在一個目錄下Installing setuptools, pip, wheel...done.(testenv3) c:\Users\liubin\Envs\testenv2\Scripts>workon  //查看當前有哪些虛擬環境Pass a name to activate one of the following virtualenvs:==============================================================================testenv2testenv3(testenv3) c:\Users\liubin\Envs\testenv2\Scripts>workon testenv2  //workon也可以用來切換虛擬環境(testenv2) c:\Users\liubin\Envs\testenv2\Scripts>



python之virtualenv的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.