Three Python Tips

Quick and easy

by Martin Brochhaus / @mbrochh

Tip #1: Use virtualenv

For newcomers this is should be the first thing to learn about the Python ecosystem

Tell me why?!

  • Avoid dependency hell
  • Create isolated environments
  • Don't pollute your global environment
  • Create reproducible environments

Tell me how!!


pip install virtualenv
cd ~/Projects/myproject
virtualenv venv
source venv/bin/activate
pip install requests
deactivate
				        

Tell me more!

  • Creates a folder `venv`
  • Uses it's own Python executable from that folder
  • Installs packages into that folder
  • Don't forget to add that folder to your .gitignore
  • See https://pypi.python.org/pypi/virtualenv

Tip #2: Use virtualenvwrapper

Virtualenv alone is nice.
Combined with virtualenvwrapper, it is awesome.

Tell me why?!

  • Have all your virtual environments in one folder
  • Activate them from anywhere in your terminal
  • Support for auto-complete of environment names

Tell me how!!


# In terminal:
pip install virtualenvrapper
					    

# In .bash_profile
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
export WORKON_HOME=$HOME/.virtualenvs
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true
source /usr/local/bin/virtualenvwrapper.sh
				        

# In terminal:
source ~/.bash_profile
mkvirtualenv myproject
workon myproject
deactivate
					    

Tell me more!

Tip 3: Lint your code

Python has wonderful code conventions. Just stick to them, be one of us :)

Tell me why?!

  • Don't waste time to create your own code conventions
  • Finds nasty syntax errors for you
  • Keeps your code beautiful

Tell me how!!


workon myproject
pip install flake8
source ~/.bashrc
flake8 .
				        

Tell me more!

The End