[Mac][Python][Virtualenv]安裝配置和使用


安裝幫助文檔安裝VirtualEnv報錯如下

tekiMacBook-Air:workspaces hbai$ source /usr/local/bin/virtualenvwrapper.sh
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks. 

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
tekiMacBook-Air:workspaces hbai$ which python
/usr/bin/python

檢查后發現因為Mac本機自帶的python2.7 安裝在/usr/bin 下面,但是安裝了pycharm后會在/usr/local/bin會有python3.0的執行文件,所以本地的調用基本都被pycharm接管了。

因此給py2.7安裝相對應的環境時候,需要通過pycharm來進行安裝virtalenv 和 virtualenvwrapper才可以

 

 

安裝完畢后 再在命令行執行即可

  • 直接創建虛擬環境 $ virtualenv env1
  • 導入環境變量以后通過mkvirtualenv 創建虛擬環境
    •   $export WORKON_HOME=~/workspaces
    •   $source /usr/local/bin/virtualenvwrapper.sh
    •   $mkvirtualenv env1

通過這種方式創建的python環境就是3.4的了

testtekiMacBook-Air:workspaces test$ virtualenv env1
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.4'
New python executable in /Users/test/workspaces/env1/bin/python3.4
Also creating executable in /Users/test/workspaces/env1/bin/python
Installing setuptools, pip, wheel...done.
testtekiMacBook-Air:workspaces test$ ls
env1
testtekiMacBook-Air:workspaces test$ rm -rf env1/ testtekiMacBook
-Air:workspaces test$ export WORKON_HOME=~/workspaces testtekiMacBook-Air:workspaces test$ source /usr/local/bin/virtualenvwrapper.sh virtualenvwrapper.user_scripts creating /Users/test/workspaces/initialize virtualenvwrapper.user_scripts creating /Users/test/workspaces/premkvirtualenv virtualenvwrapper.user_scripts creating /Users/test/workspaces/postmkvirtualenv virtualenvwrapper.user_scripts creating /Users/test/workspaces/prermvirtualenv virtualenvwrapper.user_scripts creating /Users/test/workspaces/postrmvirtualenv virtualenvwrapper.user_scripts creating /Users/test/workspaces/predeactivate virtualenvwrapper.user_scripts creating /Users/test/workspaces/postdeactivate virtualenvwrapper.user_scripts creating /Users/test/workspaces/preactivate virtualenvwrapper.user_scripts creating /Users/test/workspaces/postactivate virtualenvwrapper.user_scripts creating /Users/test/workspaces/get_env_details virtualenvwrapper.user_scripts creating /Users/test/workspaces/premkproject virtualenvwrapper.user_scripts creating /Users/test/workspaces/postmkproject testtekiMacBook-Air:workspaces test$ ls get_env_details postactivate postmkproject postrmvirtualenv predeactivate premkvirtualenv initialize postdeactivate postmkvirtualenv preactivate premkproject prermvirtualenv testtekiMacBook-Air:workspaces test$ mkvirtualenv env1 Using base prefix '/Library/Frameworks/Python.framework/Versions/3.4' New python executable in /Users/test/workspaces/env1/bin/python3.4 Also creating executable in /Users/test/workspaces/env1/bin/python Installing setuptools, pip, wheel...done. virtualenvwrapper.user_scripts creating /Users/test/workspaces/env1/bin/predeactivate virtualenvwrapper.user_scripts creating /Users/test/workspaces/env1/bin/postdeactivate virtualenvwrapper.user_scripts creating /Users/test/workspaces/env1/bin/preactivate virtualenvwrapper.user_scripts creating /Users/test/workspaces/env1/bin/postactivate virtualenvwrapper.user_scripts creating /Users/test/workspaces/env1/bin/get_env_details (env1) testtekiMacBook-Air:workspaces test$ python Python 3.4.0 (v3.4.0:04f714765c13, Mar 15 2014, 23:02:41) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>

 

注: 如果需要使用mac原先自帶的py2.7的環境進行創建virtual env,步驟如下:

需要指定pip3的安裝以及設置VIRTUALENVWRAPPER的工作路徑,但是不推薦,因為這樣運行環境是py2.7的

$ /usr/local/bin/pip3 install virtualenv virtualenvwrapper
$ export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
$ source /usr/local/bin/virtualenvwrapper.sh

 

常用命令:

  1. 列出虛擬環境:

    $ lsvirtualenv -b
    env1
    env2
  2. 切換虛擬環境:

    $ workon env1
  3. 查看環境里安裝了哪些包:

    $ lssitepackages
  4. 進入當前環境:

    $ cdvirtualenv
  5. 進入當前環境的site-packages:

    $ cdsitepackages $ cdsitepackages pip
  6. 復制虛擬環境:

    $ cpvirtualenv env1 env3
    Copying env1 as env3...
  7. 退出虛擬環境:

    $ deactivate
  8. 刪除虛擬環境:

    $ rmvirtualenv env2 Removing env2...

 

參考文檔: http://www.jianshu.com/p/51140800e8b4#

http://stackoverflow.com/questions/29486113/problems-with-python-and-virtualenvwrapper-after-updating-no-module-named-virtu

http://stackoverflow.com/questions/27308234/usr-bin-python-vs-opt-local-bin-python2-7-on-os-x

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM