I will summarize the environment construction of python for the future.
--Version control (pyenv) --Environment management (virtualenv) --Also use anaconda (anaconda: useful python package) --Also touch on iPython notebook (development must item if you don't know)
First, install pyenv and virtualenv with homebrew. (Homebrew is assumed to be installed.)
You can manage the version with pyenv. You can manage each environment such as development environment with virtualenv.
$ brew install pyenv
$ brew install pyenv-virtualenv
Then pass the path. Describe the following in .bash_profile
. Then, it will be loaded automatically when the terminal is started. (If the login shell is not bash, please take appropriate action)
~/.bash_profile
PYENV_ROOT=~/.pyenv
export PATH=$PATH:$PYENV_ROOT/bin
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
By passing the path, you will be able to execute the command as follows.
$ pyenv
Load .bash_profile
once.
$ source ~/.bash_profile
Now, let's install python.
$ pyenv install --list
This will give you a list of installable python versions. You can install any version of python from this. Here, we will install anaconda. Anaconda is very convenient because it sets up and prepares Python itself and frequently used packages together. This is easier than installing python alone.
$ pyenv install anaconda3-2.4.1
This command takes a long time to complete. This will install anaconda with various packages installed on Python 3.
Check the version currently in use.
$ pyenv versions
If you have ʻanaconda3-2.4.1as below, it's okay. The version with
* is the version you are currently using, so switch to ʻanaconda3-2.4.1
.
* system
anaconda3-2.4.1 (set by /Users/your_name/.pyenv/version)
$ pyenv global anaconda3-2.4.1
$ pyenv versions
system
* anaconda3-2.4.1 (set by /Users/your_name/.pyenv/version)
Now that you can use python, let's also install python2 anaconda.
$ pyenv install anaconda-2.4.0
system
anaconda-2.4.0
* anaconda3-2.4.1 (set by /Users/your_name/.pyenv/version)
After that, you can switch at any time as follows. Since there are situations where it is necessary to use python2 and python3 properly, I think it is better to prepare both. You may have felt the strength of pyenv
, which allows you to easily switch versions.
$ pyenv global anaconda-2.4.0
By installing anaconda, you can use your favorite version of python. Let's briefly touch on ʻipython notebook`, which you must know if you develop with python.
After installing anaconda, the environment is already in place.
$ ipython notebook
When you type, the browser will be launched.
You can create a new file from "New"> "Python3" in the upper right corner of your browser.
When the file opens, you can write python code and press "Shift + enter" to execute the code in the cell. You can easily draw a graph with the following feeling.
It's a very powerful tool in development, so it's a good idea to have it available.
I posted a post about data analysis before, but this is all done on the ipython notebook. Practical exercise of data analysis with Python ~ 2016 New Coder Survey Edition ~
When developing a team, it is necessary to unify the development environment with the members. In such a case, I think it is better to set up for each environment.
First, install a common version of python. (Here 3.5 series)
$ pyenv install 3.5.1
$ pyenv virtualenv 3.5.1 hoge
This will create an environment named "hoge" in 3.5.1. Then change to the directory that specifies the environment. Then specify the version in it.
$ pyenv local hoge
By doing this, the environment of "hoge" is applied in this directory.
This completes the environment settings.
Note that unlike anaconda, it's pure python, so you need to install the required packages.
Let's install the necessary packages as follows. The installation of numpy
below is an example.
$ pip install numpy
Now, in order to unify the environment with others, you also need to synchronize the version of the installed package. Now let's output your own environment.
$ pip freeze > requirements.txt
The file name on the right side of pip freeze
is the output file name. Generally, it seems to be output to requirements.txt
, so I think it is better to use this name.
For example, I installed only numpy and output it to requirements.txt.
$ pip install numpy
$ pip freeze > requirements.txt
$ cat requirements.txt
numpy==1.11.1
Others install the package from this file.
$ pip install -r requirements.txt
This will install the packages described in the file. I often see it in the README of the github repository where I install the package.
Finally, there is a way to remove this virtual environment.
$ pyenv uninstall hoge
You can now delete the created virtual environment.
Recommended Posts