The definitive guide to setup my Python workspace

What are my setup requirements?

  1. I need CPython 2.7 and 3.6, but I want to be able to install other interpreters like PyPy and Anaconda;
  2. Python3 must be the default for EVERYTHING, but easily step back when I need Python2.
  3. I want one Jupyter Notebook that works both with Python2 and Python3, and both are able to detect the active virtualenv at jupyter notebook execution time.
  4. I want one iPython Console for Python3 and one iPython Console for Python2, so no need to install iPython on my projects’ virtualenvs.
  5. I want useful programs written in Python (ex: youtube-dl) globally available on my system without contaminating the global interpreters and avoiding any library version issues.
  6. I want to use virtualenvwrapper to develop my projects allowing me to change context/project quickly with one command.
  7. I want this setup to be maintainable without adding too many thing to PATH.

What is the best way to install Python on a Unix?

  1. I use pyenv to install Python interpreters;
  2. I use pyenv-virtualenv to configure my “global environment”;
  3. I use pyenv-virtualenvwrapper to work on my projects;
brew install pyenv
brew install pyenv-virtualenv
brew install pyenv-virtualenvwrapper
# All virtualenvs will be on...
mkdir ~/.ve
# All projects will be on...
mkdir ~/workspace
export WORKON_HOME=~/.ve
export PROJECT_HOME=~/workspace
eval "$(pyenv init -)"
#pyenv virtualenvwrapper_lazy
  1. Note that there is a # at the beginning of the last line to keep pyenv-virtualenvwrapper from starting at this first moment. We’ll get back to it later.
  2. Also note that I did not include the command pyenv virtualenv init in ~/.bashrc unlike the documentation suggests. This is on purpose: activate both extensions causes conflicts.
pyenv install 3.6.0
pyenv install 2.7.13

Resist the temptation to contaminate your global Python install

pyenv virtualenv 3.6.0 jupyter3
pyenv virtualenv 3.6.0 tools3
pyenv virtualenv 2.7.13 ipython2
pyenv virtualenv 2.7.13 tools2
pyenv activate jupyter3
pip install jupyter
python -m ipykernel install --user
pyenv deactivate
pyenv activate ipython2
pip install ipykernel
python -m ipykernel install --user
pyenv deactivate
pyenv activate tools3
pip install youtube-dl gnucash-to-beancount rows
pyenv deactivate
pyenv activate tools2
pip install rename s3cmd fabric mercurial
pyenv deactivate
pyenv global 3.6.0 2.7.13 jupyter3 ipython2 tools3 tools2
~$ pyenv which python
/Users/henrique/.pyenv/versions/3.6.0/bin/python

~$ pyenv which python2
/Users/henrique/.pyenv/versions/2.7.13/bin/python2

~$ pyenv which jupyter
/Users/henrique/.pyenv/versions/jupyter3/bin/jupyter

~$ pyenv which ipython
/Users/henrique/.pyenv/versions/jupyter3/bin/ipython

~$ pyenv which ipython2
/Users/henrique/.pyenv/versions/ipython2/bin/ipython2

~$ pyenv which youtube-dl
/Users/henrique/.pyenv/versions/tools3/bin/youtube-dl

~$ pyenv which rename
/Users/henrique/.pyenv/versions/tools2/bin/rename

What about my projects’ virtualenvs?

  1. Say I want to start a new project proj3 with Python3. Running mkproject proj3 will create a virtualenv with Python3 (default) at ~/.ve/proj3 and an empty project directory at ~/workspace/proj3 .
  2. Now say I just opened the terminal and want to work on my new proj3. Running workon proj3 will activate the virtualenv ~/.ve/proj3 and change the current directory to ~/workspace/proj3.
  3. Let’s say I just cloned a project called proj2 which runs on Python2 at ~/workspace/proj2 and I need a virtualenv to work on it. Running mkvirtualenv -a ~/workspace/proj2 -p python2 proj2 will create a virtualenv with Python2 at ~/.ve/proj2 associating to it the project directory ~/workspace/proj2. Then running workon proj2 will activate the virtualenv and change the directory to the project’s path.

How to use Jupyter and iPython with my projects?

ipython profile create
curl -L http://hbn.link/hb-ipython-startup-script > ~/.ipython/profile_default/startup/00-venv-sitepackages.py

--

--

--

Eu crio e invisto em iniciativas que promovem a autonomia das pessoas | Me encontre em: https://linktr.ee/henriquebastosnet

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Henrique Bastos

Henrique Bastos

Eu crio e invisto em iniciativas que promovem a autonomia das pessoas | Me encontre em: https://linktr.ee/henriquebastosnet

More from Medium

Python for kids

A quick method to cross-cluster data copying with a Python script

Finally finished a weekend project

Build a WORDLE bot using python [part — 2]