Last time Using multiple versions of Python on Mac OS X (1) Multiple Ver installation Described how to install Python for each version using homebrew.
Now, let's use them concretely to create a Python execution environment.
I think there are various development styles, but for Python, I think that "using virtualenv to build and use a unique environment for each project without using a global environment" is currently easier. I will.
Since # Python3.3 has officially made it possible to build individual environments, we may move to that one in the future.
It is a tool that allows you to build a number of Python execution environments that are independent of virtualenv, apart from the global ones. Not only the Python version but also various necessary libraries can be created separately for each environment, so prepare an environment created with virtualenv for each project and run the program in the environment under virtualenv including commercial deployment. I think there are many cases.
virtualenv can specify a Python interpreter separate from its own installation environment. In short, it is possible to do something like "Create a Python3 virtual environment with a virtualenv installed in a Python2.7 environment", so there is no need to put a virtualenv in both Python2 and 3 systems.
This time as well, put virtualenv only in 2.7 and do the subsequent work.
[~] pip install virtualenv
This one line is the end. pip is a python library management tool. Virtualenv has been installed under the environment of python2.7.
After installation, you can create a new environment with the following command.
[~] virtualenv --python=/usr/local/bin/python3 testenv
The Path of the Python interpreter you want to use is specified in the --python option (this time Python3.3 installed from homebrew last time).
testenv is the name of the unique environment created this time.
When this command is executed, a folder called testenv is created directly under the executed directory, and an independent Python execution environment is packed in it.
[~] source testenv/bin/activate
(testenv)[~]← Entered the testenv environment
(testenv)[~] python --version
Python 3.3.2
(testenv)[~] deactivate
[~]← Exited the testenv environment and returned to the global environment
This is just one line. You can enter the testenv environment created this time just by executing activate under (virtual environment name) / bin. It's easy to understand because the environment you are in is displayed in parentheses.
Notice that the Python version is that of the interpreter specified by --python. You can deactivate when you exit.
that's all!
Currently, there is no specific project, I just want to play with Python, but ... for the time being
However, I think that you should give an environment name and install various libraries in it. Library installation
pip install (Library name)
so.
sudo pip install (Library name)
Please note that even though the environment has been cut off uniquely, it will enter the global market.
#bonus There is also a wrapper that can manage virtualenv, such as virtualenvwrapper, with a single workon command.
Is possible. If you create the environment in a directory somewhere, it will not be so complicated and I think it is simple enough, so I do not use it, but if you are interested, please search. </ del>
The previous statement is withdrawn (^^; ゞ
It's convenient, so if you are interested, please see this article for the installation method and usage.
Recommended Posts