Python

From YobiWiki
Revision as of 22:25, 15 October 2008 by <bdi>PhilippeTeuwen</bdi> (talk | contribs) (New page: ==Misc links== *absolute relative imports: links with some info ** http://bugs.python.org/issue1510172 ** http://www.python.org/dev/peps/pep-0366/ ** http://www.python.org/dev/peps/pep-03...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Misc links

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