Python

From YobiWiki
Jump to navigation Jump to search

Misc links

Installing another Python version

When installing a python version not available as a package for your linux distribution:

$ ./configure
$ make
# make altinstall

this newly installed python version will then be available in your path as pythonX.X (with X.X being the python version)

When you want to be able to use setuptools, you also need zlib. zlib will be compiled with python if the zlib-dev package is available on your system. On debian/ubuntu:

# apt-get install zlib1g-dev

and then install python. Python setup will find zlib by itself.
Installing setuptools:

  • download the appropriate egg
  • install it by running it as a shell script
# sh setuptools-x.x-pyx.x.egg


Python can be installed with every module available on your systems python install by first doing:

# apt-get build-dep python2.5

Setup Script

distutils vs setuptools

Dependencies checking on install: some info here.

It seems that the 'requires' keyword in distutils has only a purpose of documentation, but 'install_requires' in setuptools really takes care of dependencies: availability of dependencies will be checked. If a package is not available, it will be checked for on pypi and installed automatically.

distutils

  • setup.py
    • creating a distribution tar.gz: "python setup.py sdist"
    • installing the source distribution (sdist):
      - untar .tar.gz: "cd dist && tar zxfv CryptoPlus-1.0.tar.gz -C ~/"
      - "cd ~/CryptoPlus-1.0 && python setup.py install"
    • "egg way":
      $ python setup.py bdist_egg
      $ sudo easy_install dist/CryptoPlus-1.0-py2.5.egg

setuptools

Installing development Python packages


During development, it's painful to install the package every time you're doing sth on it.
There are various ways to import a module actually

Basic import from a local directory

In your code, e.g. mycode.py:

from Module1 import Function1

Usage: pointing to the path containing the module

PYTHONPATH=/path/to/src/directory/above/Module1 python mycode.py

Import from a local directory, using egg_info

This is using python-pkg-resources

Installing (locally):

python setup.py egg_info

In your code, e.g. mycode.py:

from pkg_resources import require
require("Module1>=1.0")
from Module1 import Function1

Usage: pointing to the path containing the egg_info

PYTHONPATH=/path/to/src/directory/containing/egg_info python mycode.py

Cleaning:

rm -rf /path/to/src/directory/containing/egg_info/Module1.egg-info

Install a development version

Actually symlinking to the working directory
This is using python-pkg-resources

Installing (symlink):

sudo python setup.py develop

Usage:

python mycode.py

Cleaning:

sudo python setup.py develop --uninstall
rm -rf /path/to/src/directory/containing/egg_info/Module1.egg-info

Create and installing an egg

This is using python-pkg-resources

Installing:

python setup.py bdist_egg
sudo easy_install dist/Module1-1.0-py2.5.egg

Usage:

python mycode.py

Cleaning:

sudo rm /usr/lib/python2.5/site-packages/Module1-1.0-py2.5.egg

Create and using locally an egg

This is using python-pkg-resources

Creating:

python setup.py bdist_egg

In your code, e.g. mycode.py:

from pkg_resources import require
require("Module1>=1.0")
from Module1 import Function1

Usage: pointing to the path containing the egg file

PYTHONPATH=/path/to/your/dist/ python mycode.py