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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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

 




以上是关于[Mac][Python][Virtualenv]安装配置和使用的主要内容,如果未能解决你的问题,请参考以下文章

Mac 安装虚拟环境 python virtualenv virtualenvwrapper

Mac OS 中使用virtualenv解决python冲突

linux和mac使用virtualenv使用和安装

Mac 安装配置Python3虚拟环境(VirtualEnv)及virtualenvwrapper扩展包

OpenCV可以安装在Mac Mountain Lion上的python virtualenv中吗

markdown 在Mac上设置和使用Python3 Virtualenv