python virtualenv & pip in 90 minutes

13
Larry cai <larry.caiyu#gmail.com>

Upload: larry-cai

Post on 08-May-2015

2.251 views

Category:

Education


3 download

DESCRIPTION

learn Python virtualenv & pip in 90 minutes by doing exercise

TRANSCRIPT

Page 1: Python virtualenv & pip in 90 minutes

Larry cai <larry.caiyu#gmail.com>

Page 2: Python virtualenv & pip in 90 minutes

Agenda

Python Virtualenv & Pip in 90 minutes

2

What is Virtualenv & Pip ? Environment : Install Virtualenv 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

Page 3: Python virtualenv & pip in 90 minutes

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 minutes

3

Page 4: Python virtualenv & pip in 90 minutes

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 minutes

4

Page 5: Python virtualenv & pip in 90 minutes

Environment

Python Virtualenv & Pip in 90 minutes

5

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

Install Virtualenv pip --version pip install virtualenv

http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows

Page 6: Python virtualenv & pip in 90 minutes

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 minutes

6

Page 7: Python virtualenv & pip in 90 minutes

Exercise 1: Create own environment

Python Virtualenv & Pip in 90 minutes

7

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

Page 8: Python virtualenv & pip in 90 minutes

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 minutes

8

http://www.pip-installer.org/en/latest/usage.html

Page 9: Python virtualenv & pip in 90 minutes

Exercise 3: Create your own package Write a simple hello.py under hello folder

print “hello world”

Write a setup.pyfrom 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 minutes

9

http://docs.python.org/2/distutils/introduction.html

Page 10: Python virtualenv & pip in 90 minutes

Exercise 4: make your scripts runnable Make the hello.py can be runnable after

installationfrom 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 minutes

10

Page 11: Python virtualenv & pip in 90 minutes

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 minutes

11

Page 12: Python virtualenv & pip in 90 minutes

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 minutes

12

Page 13: Python virtualenv & pip in 90 minutes

Reference Slides

http://www.slideshare.net/webdebs/virtualenv-12727213

Usage link http://www.pip-installer.org/en/latest/usage.html https://pypi.python.org/pypi http://docs.python.org/2/distutils/introduction.html https://pypi.python.org/pypi/virtualenv

Python Virtualenv & Pip in 90 minutes

13