Using different versions of python with virtualenvwrapper

后端 未结 5 1907
天涯浪人
天涯浪人 2021-01-30 16:49

I\'ve got various versions of python installed on my Mac using Macports. When I\'ve selected python 2.7 via $ port select python python27, virtualenvwrapper works p

5条回答
  •  感情败类
    2021-01-30 17:33

    None of these worked. I installed Python3 first when setting up my osx machine, and pip and all default to that.

    First, check which python you have installed:

    $ `which python` -V
    

    If this returns "Python 2.7.12", then you are set to run:

    $ mkvirtualenv -p `which python` api_server
    Running virtualenv with interpreter /usr/local/bin/python
    New python executable in /Users/eric/.virtualenvs/api_server/bin/python2.7
    Also creating executable in /Users/eric/.virtualenvs/api_server/bin/python
    Installing setuptools, pip, wheel...done.
    virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/predeactivate
    virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/preactivate
    virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/postactivate
    virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/get_env_details
    

    This will also activate the api_server workon, which changes your python executable:

    $ which python
    /Users/eric/.virtualenvs/api_server/bin/python
    $ python -V
    Python 2.7.12
    

    What does which python actually do? It outputs the directory of the python executables found in your PATH:

    $ which python
    /usr/local/bin/python
    

    By using which python, you are basically passing in /usr/local/bin/python to the -p option in the mkvirtualenv directory.

    What happens when you have more than one python executable returned in which python? Just find the one you want and pass it in:

    $ mkvirtualenv -p /usr/local/bin/python3 api_server
    

    And virtualenvwrapper will end up using that python executable instead.

提交回复
热议问题