2. Agenda
Python Virtualenv & Pip in 90 minutes2
What isVirtualenv & Pip ?
Environment : InstallVirtualenv on windows (Linux is
easy)
Exercise 1: Create own environment using virtualenv
Exercise 2: Learn pip command
Exercise 3: Create own Pip package
Exercise 4: Make scripts runnable directly
Bonus Exercise: Share your scripts outside
!!!! Exercise are created by me, and some introduction slides are
copied from http://www.slideshare.net/webdebs/virtualenv-12727213
3. Problem for using python
How to install the packages ?
(download & python setup.py install)
How to use different python versions (2.6,2.7,3.x..)?
How to install different packages (0.2,0.3.)
How to test some packages without ruin the system
How to delivery your python codes to other ?
How to verify them ?
…
Python Virtualenv & Pip in 90 minutes3
4. What is Pip & PyPi & Virtualenv
pip is a tool for installing and managing Python packages
It’s a replacement for easy_install.
PyPi (Python Pakage Index) is a repository of software for
Python and currently count 33429 package
https://pypi.python.org/pypi
Virtualenv is a tool to isolate your python environment
Python Virtualenv & Pip in 90 minutes4
5. Environment
Python Virtualenv & Pip in 90 minutes5
Python 2.7.x In Windows with Git Bash
http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi (2.7.5
has issues with pycrypto)
Add into Path
Install Pip (Python package management)
curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
easy_install pip
InstallVirtualenv
pip --version
pip install virtualenv
http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows
6. WHY virtual environments ?
Isolation - Python packages and even version live in their
own planet :)
Permissions - No sudoers, the environment is mine!!!
Organization - each project can maintain its own
requirements file of Python packages.
No-Globalization – don’t require installing stuff globally
on the system.
Python Virtualenv & Pip in 90 minutes6
7. Exercise 1: Create own environment
Python Virtualenv & Pip in 90 minutes7
Create own environment
$ pip list # if error, $ pip install --upgrade setuptools
$ cd ~
$ virtualenv venv
$ find venv
$ . venv/Scripts/activate
$ pip list
$ pip install python-jenkins xunitparser
$ pip list
$ find ~/venv | grep unit
$ deactivate
$ pip list
8. Exercise 2: learn pip
Install , uninstall, upgrade packages
$ . ~/venv/Scripts/activate
$ pip install junit-xml # latest version
$ pip uninstall junit-xml
$ pip install junit-xml==1.2 # specific version
$ pip install --upgrade junit-xml
Generate package list and restore
$ pip list
$ pip freeze > requirements.txt
$ pip uninstall junit-xml xunitparser
$ pip install -r requirements.txt
Install local packages from mirror
$ pip install --index-url http://my.package.repo/simple/ <yourpackage>
Python Virtualenv & Pip in 90 minutes8
http://www.pip-installer.org/en/latest/usage.html
9. Exercise 3: Create your own package
Write a simple hello.py under hello folder
print “hello world”
Write a setup.py
from distutils.core import setup
setup(name=‘hello',
version=‘0.0.1',
py_modules=[‘hello'], )
Package and install
$ python setup.py sdist
$ pip install dist/hello.0.0.1.zip
$ find ~/venv | grep hello
Change version to “0.0.2” and do it again
Python Virtualenv & Pip in 90 minutes9
http://docs.python.org/2/distutils/introduction.html
10. Exercise 4: make your scripts runnable
Make the hello.py can be runnable after installation
from distutils.core import
setup setup(name=‘hello',
version=‘0.0.3’,
scripts=[‘hello.py’],
py_modules=[‘hello'], )
Testing it
$ python setup.py sdist
$ pip install dist/hello.0.0.3.zip
$ find ~/venv | grep hello
Make it runnable (change hello.py ..)
$ hello.py # bingo, so simple
Python Virtualenv & Pip in 90 minutes10
11. Bonus: Share your scripts outside
https://pypi.python.org/pypi register and
$ python setup.py register sdist upload # that’s all
Simpler than your thinking ….
Python Virtualenv & Pip in 90 minutes11
12. Summary
Using virtualenv to isolate your python environment
Use pip to package
Host in github and shared in PyPi repository
Enjoy python programming
Python Virtualenv & Pip in 90 minutes12