开发者

Multiple installs of Python on MacOSX for Eclipse

I want to have multiple installs of Python: 2.1, 2.4, 2.7, 3.x

My IDE is Eclipse (Helios)/Pydev on MacOSX, which works great. I have a couple of Python codebases that are/will be running on different versions of Python. Also, I like Eclipse PyDev's crosslinking from source-code to documentation.

The standard recommendation seems to be: use virtualenv, and keep the installs totally separate from the builtin MacPython (2.6.1). Eclipse should never be pointing to the MacPython install. (Should PYTHONPATH even be set in such an environment?)

Before I get on with virtualenv, is there anything else I should know about this? virtualenv doesn't impose any overhead, and I shouldn't be worried with occasional comments about breakage to nose, coverage etc?

I'm interested in hearing from Eclipse or Pydev users on MacOS.

Also if anyone h开发者_运维技巧as other tips on migrating a Python codebase from 2.1 -> 2.7.


A good way is to use macport to install the different version of pythons. It will have different versions of all packages for all versions of pythons that you want. They will be installed in /opt/local/. So in Eclipse with PyDev for a particular project you can right click into the name of the project -> properties: There, in the left you choose PyDev - Interpreter/Grammar. And you click the link Click here to configure an interpreter not lister. You click on new on the top right of the new window. You give the name of the version of python you want to create the interpreter for, if you have already installed it with all required package through macport. And in the field Intepreter Executable you give the path: /opt/local/bin/pythonX.X . After, in the previous option window: PyDev - Intepreter/Grammar, you can choose in the Interpreter menu the python version that you just installed.

For the shell, and the default path pointing to /usr/bin/python you must use: python_select (installed through macport) instead of playing with the env variables. Afterward you can use macport to update/install new packages, make sure everything is alway clean.


Having Home Brew already installed in your system, I recommend you pyenv. Most of the following information comes from this quick installation/use guide:

Installing pyenv + pyenv-virtualenv

  1. Go to your terminal and install pyenv and the external libraries needed by Python:

    brew update
    brew install pyenv openssl readline sqlite3 xz zlib
    
  2. Add pyenv init to your shell to enable shims and autocompletion running the following command:

    echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
    
  3. Install the pyenv-virtualenv plugin so you can create virtual environments for each version of Python:

    git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
    
  4. Reset your terminal to apply the changes exec "$SHELL" or just close your terminal and open a new one.

Gettings different Python versions

We can list all available versions with pyenv install --list:

pyenv install --list
Available versions:
  2.7.15
  3.0.1
  ..
  3.7.2

For installing the Python version 3.7.2:

pyenv install 3.7.2

With pyenv versions you can see all the Python versions installed in your computer:

root@Host ~$ pyenv versions
* 3.7.2

Creating a virtual environment

In the previous step we have downloaded the Python 3.7.2 interpreter, we can now use it to create an isolated virtual environment. This is very useful for software development, keeping each project completely isolated from the others:

pyenv virtualenv 3.7.2 MY_VIRTUALENV_NAME

To list all your virtual environments:

pyenv virtualenvs

To activate a virtual environment:

pyenv activate MY_VIRTUALENV_NAME

To deactivate the currently active virtual environment:

pyenv deactivate


From the README text file of python

Installing multiple versions

On Unix and Mac systems if you intend to install multiple versions of Python using the same installation prefix (--prefix argument to the configure script) you must take care that your primary python executable is not overwritten by the installation of a different version. All files and directories installed using "make altinstall" contain the major and minor version and can thus live side-by-side. "make install" also creates ${prefix}/bin/python which refers to ${prefix}/bin/pythonX.Y. If you intend to install multiple versions using the same prefix you must decide which version (if any) is your "primary" version. Install that version using "make install". Install all other versions using "make altinstall".

For example, if you want to install Python 2.5, 2.6 and 3.0 with 2.6 being the primary version, you would execute "make install" in your 2.6 build directory and "make altinstall" in the others.

Virtualenv is an option but you could use the above mentioned option instead of venv which seems much simpler.


Personally, I use conda to create multiple environments (mostly, you create a new env, activate it and install the packages you want there -- using conda itself if possible or pip if it's not available in conda). See: https://conda.io/docs/installation.html.

After you have an environment created, you just need to add a new interpreter inside PyDev which points to the interpreter in the created environment. Ssee: http://www.pydev.org/manual_101_interpreter.html.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜