data science tutorials and snippets prepared by tomis9
these three Python packages let you install your favourite Python version with your favourite Python packages’ versions on any machine independently to those already installed on the system; you can even store many Python versions and Python packages’ versions;
pyenv let’s you install any Python version you like;
virtualenv let’s you install any Python’s package version you like;
freeze informs you what packages and which versions of them are already installed. It’s very useful when you want to create the same Python environment on any other machine.
Installation is trivial. All you have to do is to clone two repositories from github (just copy-paste the code):
pyenv
cd
git clone git://github.com/yyuu/pyenv.git .pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc
virtualenv
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
source ~/.bashrc
Everything is described in “definitely the best tutorial”; link available in section 4.
Say you want to create a new project with Python 3.6.0.
pyenv install 3.6.0
and you want to write an application which uses numpy and flask. First you have to decide how you’re going to call your app. And then create a virtualenv with that name.
pyenv virtualenv 3.6.0 awesome_app
In order to install packages for this specific Python version and use it’s interpreter, let’s activate our brand new environment:
pyenv activate awesome_app
As you can see, the prompt has changed. Now you can install any package you want with pip:
pip install --upgrade pip # this may help at the beginning
pip install flask pytest
and check the Python’s version if it is actually 3.6.0:
python --version
Finally, you can list all the packges that are installed under this Python’s version:
pip freeze
It is a good practice to keep the list of you packages in a requirements.txt
file
pip freeze > requirements.txt
so you can easily install them with
pip install -r requirements.txt
when you download your repo from a remote repository.
When the work is done, type
pyenv deactivate
pyenv virtualenvs
lists all existing virtualenvs