Make a note of it as you need it when working remotely
The referenced site may be easier to understand
The environment is mac OSX: Yosemite
.
pyenv pyenv manages the python environment. You can create a python environment that is different from the standard environment.
Reference URL: How to use pyenv and virtualenv How to manage your Python environment with pyenv Try using pyenv and virtualenv
https://github.com/KodairaTomonori/Qiita/blob/master/shell/construct_pyenv.sh Copy this and run it to build the environment at home If you want to build other than home, you can do it by rewriting ~ / part to any path.
First, git clone
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Then write the following in `~ / .bash_profile`. (About .bashrc, .bash_profile: [Really correct use of .bashrc and .bash_profile](http://qiita.com/magicant/items/d3bb7ea1192e63fba850)
export PYENV_ROOT="
\ * 2020/03/17 Fixed: Double quart added
## Installation of each version of python
You can install it with `pyenv install 3.5.0`. The version is optional.
#### **`install-version.`**
$ pyenv install 3.5.0 $ pyenv install 2.7.9
If you make a mistake, change ʻinstall` to ʻuninstall` and execute.
## Switching python version
You can switch the version with `pyenv global x.x.x` or` pyenv local x.x.x`.
If you set it to `global`, it will be reflected in the whole, and if you set it to` local`, it will be reflected in the current directory. This reflection will continue even if you exit terminal, so it is very convenient to set it for each folder with `local`.
Changes in that shell can be made with `shell`. If you use this, the environment will only be reflected in the shell you are currently using. Therefore, if you close the shell and then reopen it, or if you open multiple shells, the environment change will not be affected by others.
Is it `shell` if you want to use it temporarily,` local` if you want to set it for each folder, or `global` if you want to change the overall default environment?
You can restore it by changing the `x.x.x` part to` system`.
#### **`swich-python-version.`**
python --version Python 2.7.3
$ pyenv shell 2.7.9 $ python --version Python 2.7.9
$ pyenv shell 3.5.0 $ python --version Python 3.5.0
$ pyenv shell system $ python --version Python 2.7.3
## module installation
The installed python (2.7.9 and 3.5.0) contains only the default module (pip freeze does not contain anything), so install it.
To install a module, just install any module with `pip install` as usual.
```pip install xxxxx```
pyenv-virtualenv
`pyenv` alone is convenient enough, but more convenient.
`pyenv-virtualenv` is a plugin for pyenv to create different environments with the same (python) version.
## Installation method
#### **`$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv`**
Then write the settings to .bash_profile
eval "$(pyenv virtualenv-init -)"
To create a new python environment, set ``` pyenv virtualenv [version] `` to create a new environment called
virtualenv-name. For
[version]` here, specify the existing environment.
make_new.
pyenv virtualenv 3.5.0 new_3.5.0
Here, in the newly created environment, the modules installed by pip
etc. in the environment created by pyenv cannot be used.
If you want to use the one used in [version]
, add the option --system-site-package
.
Then, pass the path to the site-packages
of the one built with pyenv
.
In the example below, if you add --system-site-packages
, ~ / .pyenv / versions / 3.5.0 / lib / python3.5 / site-packages
will be added to the path.
sample_site-packages.
$ pyenv virtualenv 3.5.0 inde-3.5.0
$ pyenv virtualenv --system-site-packages 3.5.0 co-3.5.0
$ pyenv virtualenv --system-site-packages inde-3.5.0 co-inde-3.5.0
# sys.path Some parts are omitted.
$ python -c 'import sys; print(sys.path)' #Here is 3.5.0
['', '~/.pyenv/versions/3.5.0/lib/python35.zip',..., '~/.pyenv/versions/3.5.0/lib/python3.5/site-packages']
(inde-3.5.0) $ python -c 'import sys; print(sys.path)'
['', '~/.pyenv/versions/3.5.0/lib/python35.zip', .... , '~/.pyenv/versions/inde-3.5.0/lib/python3.5/site-packages']
(co-3.5.0) $ python -c 'import sys; print(sys.path)'
['', '~/.pyenv/versions/3.5.0/lib/python35.zip', ... , '~/.pyenv/versions/co-3.5.0/lib/python3.5/site-packages', '~/.pyenv/versions/3.5.0/lib/python3.5/site-packages']
(co-inde-3.5.0)kodaira@fomalhaut:~$ python -c 'import sys; print(sys.path)'
['', '~/.pyenv/versions/co-inde-3.5.0/lib/python35.zip', ... , '~/.pyenv/versions/co-inde-3.5.0/lib/python3.5/site-packages', '~/.pyenv/versions/3.5.0/lib/python3.5/site-packages']
If you want to install a module in another environment, you can write pip freeze
somewhere and install it with pip install -r
. (There seemed to be a way to copy it when doing pyenv virtualenv
, but I couldn't find it, so someone ...)
pip_install.
---In the environment you want to copy----
$ pip freeze > pyp_list.txt
---After changing the environment----
$ pip install -r pyp_list.txt
I think this is the best advantage of using pyenv-virtualenv. It's about saving the environment and recreating the same environment elsewhere.
install_wheel.
$ pyenv exec pip install wheel
Save to any directory with --wheel-dir = [dir]
Above, I used the output of pip freeze
as it is, but it seems better to use wheel
.
save_module_info.
$ pyenv exec pip freeze > pyp_list.txt
$ pyenv exec pip wheel --wheel-dir=~/tmp/wheelhouse -r pyp_list.txt
By wheel
, you can save the pre-built package, so you don't have to compile it every time.
Reference URL: I'll write it somehow.
install_using_wheel.
$ pyenv virtualenv 3.5.0 tes-3.5
$ pyenv global tes-3.5
$ pip install -r pyp_list.txt --use-wheel --no-index --find-links=tmp/wheelhouse
$ pip freeze
You can build an environment by using pyenv
and pyenv-virtualenv
.
You can save the environment with'wheel'and reproduce the same environment elsewhere.
Please let me know if there is something wrong or something more convenient
Recommended Posts