I've been using Python 2.7 for a long time, but since the language processing script I got from a person used Python 3, I tried to switch to the 3.X system.
Before switching, [Check the difference between 2.X series and 3.X series](http://postd.cc/the-key-differences-between-python-2-7-x-and-python- 3-x-with-examples /), different from what I expected.
Which one should I continue to use after all? I thought, and when I tried various things, there was a description on the Official site that "Which version to use depends almost on what you want to do". Lol.
Therefore, I decided to use pyenv, which I hated without eating.
In such a situation, I borrowed the wisdom of the pioneers and left a memo when I introduced pyenv-virtualenv in my environment.
Install pyenv and pyenv-virtualenv using Homebrew. Install Homebrew from here.
#Install pyenv
$ brew install pyenv
# pyenv-Install virtualenv
$ brew install pyenv-virtualenv
After installation, add the following to .bash_profile.
.bash_profile
$ vim ~/.bash_profile
## Set path for pyenv
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
export PATH=${PYENV_ROOT}/bin:$PATH
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
fi
Restart the terminal to reflect the settings, or type the following command.
$ source ~/.bash_profile
First check the current python version
$ python -V
Python 2.7.10
As mentioned at the beginning, it seems better to install it according to the purpose. This time, install 3.4.3 as well as the reference material.
#Create a folder and move it
$ mkdir ~/work/pyenv_test
$ cd ~/work/pyenv_test
# python3.4.Installation of 3
pyenv_test $ pyenv install 3.4.3
Downloading Python-3.4.3.tgz...
-> https://yyuu.github.io/pythons/4281ff86778db65892c05151d5de738d
Installing Python-3.4.3...
Installed Python-3.4.3 to /Users/XXXX/.pyenv/versions/3.4.3
# python3.4.Check if 3 has been introduced
pyenv_test $ pyenv versions
* system (set by /Users/XXXX/.pyenv/version)
3.4.3
pyenv_test $ ls ~/.pyenv/versions
3.4.3
At this rate, the original version of python is reflected.
$ pwd
/Users/XXXX/work/pyenv_test
$ python -V
Python 2.7.10
Therefore, use pyenv local NAME
to reflect the downloaded python version in pyenv.
$ pwd
/Users/XXXX/work/pyenv_test
$ pyenv local 3.4.3
$ pyenv versions
system
* 3.4.3 (set by /Users/XXXX/work/pyenv_test/.python-version)
Now, python3.4.3 installed by pyenv can be reflected in the pyenv_test directory.
In pyenv-virtualenv, create an environment to isolate the package to be installed.
pyenv_test $ pwd
/Users/XXXX/work/pyenv_test
#Execute the following command under the directory where you want to build the above environment
pyenv_test $ pyenv virtualenv 3.4.3 test
pyenv_test $ pyenv local test
pyenv-virtualenv: activate test
(test)$ # <-Set pyenv-virtualenv()Displayed in
(test)$ pyenv versions
system
3.4.3
* test (set by /Users/XXXX/work/pyenv_test/.python-version)
Now, every time you move to this directory, the test environment you built will be used automatically.
Manage packages using pip.
pip is installed at the same time as pyenv install NAME
.
I haven't done anything right now, so only the wheel is included.
(test) pyenv_test $ pip freeze
wheel==0.26.0
Install packages that you often use personally.
pip install numpy
pip install scipy
pip install pandas
pip install scikit-learn
pip install nltk
pip install matplotlib
pip install seaborn
pip install ipython
pip install jupyter
Some people seem to get an error with matplotlib or jupyter, but the installation is completed without any error.
You can save the installed packages as a list. Note that you can enjoy it when installing the same package in another project.
#A command to list installed packages
$ pip freeze > mypckg.list
#Command to install the listed packages
$ pip install -r mypckg.list
The built environment can be deleted with pyenv uninstall NAME
.
This deletion method does not delete the .python-version file that contains the environment name generated when local is specified.
(test) pyenv_test $ pyenv versions
system
3.4.3
* test (set by /Users/XXXX/work/pyenv_test/.python-version)
(test) pyenv_test $ pyenv uninstall 3.4.3
(test) pyenv_test $ pyenv uninstall test
pyenv: remove /Users/XXXX/.pyenv/versions/test? [[Enter y]]
pyenv-virtualenv: deactivate test
pyenv: version `test' is not installed (set by /Users/XXXX/work/pyenv_test/.python-version)
pyenv_test $ # <- ()Lose
pyenv_test $ pyenv versions
system
When building the environment from the next time onward, follow the flow below.
#python installation
$ pyenv install 3.4.3
#Environment
$ pyenv virtualenv 3.4.3 NAME
#Environment selection
$ pyenv local NAME
(NAME)$ pyenv versions
system
3.4.3
* NAME (set by /Users/XXXX/work/pyenv_test/.python-version)
#Package installation
(NAME)$ pip install -r mypckg.list
#When deleting the built environment
(NAME)$ pyenv uninstall NAME
-Building multiple Python environments on the same system -Python version control in Mavericks and use pyenv Python instead of system -Python version control -Build pyenv and virtualenv environment on Mac OSX yosemite -Pyenv + virtualenv on Mac -Notes on pyenv virtualenv -How to use pyenv and virtualenv -A story about using pyenv, which I hated without eating, was too convenient to sit down -Building Python local development environment 2 (pyenv-virtualenv, pip usage)