Upgrade to Pro — share decks privately, control downloads, hide ads and more …

90分钟:Virtualenv & pip

Zoom.Quiet
December 04, 2013

90分钟:Virtualenv & pip

Larry cai

Zoom.Quiet

December 04, 2013
Tweet

More Decks by Zoom.Quiet

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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 minutes 9 http://docs.python.org/2/distutils/introduction.html
  9. 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 minutes 10
  10. 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
  11. 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
  12. 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