Virtualenv 快速入門手冊

來源:互聯網
上載者:User

Virtualenv 快速入門手冊
1. virtualenv概述

virtualenv 通過建立獨立Python開發環境的工具,來解決依賴、版本以及間接許可權問題。比如一個項目依賴Django1.3 而當前全域開發環境為Django1.7,版本跨度過大, 導致不相容使項目無法正在運行,使用virtualenv可以解決這些問題。

virtualenv 建立一個擁有自己安裝目錄的環境,這個環境不與其他虛擬環境共用庫, 能夠方便的管理python版本和管理python庫。

2. 安裝Virtualenv

使用pip安裝Virtualenv

$ pip install virtualenv $ sudo pip install virtualenv #或者由於許可權問題使用sudo臨時提升許可權
3. virtualenv基本使用3.1 使用virtualenv建立python環境
$ virtualenv test  #建立一個名為test的目錄,並且安裝了bin/python和bin/pip,建立了lib,include,bin目錄    Using base prefix '/usr'    New python executable in /home/zhangchengfei/test/bin/python3    Also creating executable in /home/zhangchengfei/test/bin/python    Installing setuptools, pip, wheel...done.$ cd test$ ll    drwxrwxr-x  2 zhangchengfei zhangchengfei 4096  5月 24 19:37 bin/    drwxrwxr-x  2 zhangchengfei zhangchengfei 4096  5月 24 19:37 include/    drwxrwxr-x  3 zhangchengfei zhangchengfei 4096  5月 24 19:37 lib/    -rw-rw-r--  1 zhangchengfei zhangchengfei   60  5月 24 19:37 pip-selfcheck.json
  • lib,所有安裝的python庫都會放在這個目錄中的lib/pythonx.x/site-packages/下
  • bin,bin/python是在當前環境是使用的python解譯器

如果在命令列中運行virtualenv --system-site-packages test,會繼承/usr/lib/python2.7/site-packages下的所有庫,最新版本virtualenv把訪問全域site-packages作為預設行為。

3.2 啟用virtualenv
  • test目錄下使用如下命令

    $ source ./bin/activate  # 啟用當前virtualenv(test) $ # 注意終端發生了變化
  • 使用pip查看當前庫,顯示依賴並產生requirement檔案

    (test) $ pip listpip (8.1.2)setuptools (21.2.1)wheel (0.29.0) # 現在只有這三個pip freeze  # 顯示所有依賴pip freeze > requirement.txt  # 產生requirement.txt檔案pip install -r requirement.txt  # 根據requirement.txt產生相同的環境
3.3 關閉virtualenv
  • 使用下面命令

    $ deactivate
3.4 指定python版本
  • 可以使用-p PYTHON_EXE選項在建立虛擬環境的時候指定python版本

    # 建立python2.7虛擬環境$ virtualenv -p /usr/bin/python2.7 test    Running virtualenv with interpreter /usr/bin/python2.7    New python executable in /home/zhangchengfei/test/bin/python2.7    Not overwriting existing python script /home/zhangchengfei/test/bin/python (you must use /home/zhangchengfei/test/bin/python2.7)    Installing setuptools, pip, wheel...done.# 建立python3.4虛擬環境$ virtualenv -p /usr/local/bin/python3.4 test    Running virtualenv with interpreter /usr/bin/python3.4    Using base prefix '/usr'    New python executable in /home/zhangchengfei/test/bin/python3.4    Also creating executable in /home/zhangchengfei/test/bin/python    Installing setuptools, pip, wheel...done.
4. 產生可打包環境
  • 某些特殊需求下,可能沒有網路,我們期望直接打包一個test,可以解壓後直接使用,這時候可以使用virtualenv -relocatable指令將test修改為可已變更位元置的test

    # 對當前已經建立的虛擬環境更改為可遷移[test]$ virtualenv --relocatable ./    Making script /home/zhangchengfei/test/bin/wheel relative    Making script /home/zhangchengfei/test/bin/pip3 relative    Making script /home/zhangchengfei/test/bin/pip relative    Making script /home/zhangchengfei/test/bin/easy_install-3.4 relative    Making script /home/zhangchengfei/test/bin/pip3.4 relative    Making script /home/zhangchengfei/test/bin/python-config relative    Making script /home/zhangchengfei/test/bin/easy_install relative

【完】

Virtualenv建立虛擬環境安裝Flask 

Python 虛擬環境:Virtualenv 

CentOS6.5下使用Virtualenv搭建Python3開發環境 

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.