Python/pip

From Omnia
Revision as of 21:17, 28 July 2021 by Kenneth (talk | contribs) (→‎Create PIP Package)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

pip

pip - installs packages. Python packages. An easy_install replacement

pip 1.1 : Python Package Index - http://pypi.python.org/pypi/pip

Usage:

pip help
pip install [package]   # Example: 'pip install test'
pip -v --upgrade install [package]   # force upgrade
pip uninstall [package]
pip search [package]
pip freeze              # list all currently installed pip packages
pip list                # also list all packages
pip show [installed_package]      # minimal info about installed package

PIP Source Code: http://pypi.python.org/packages/source/p/pip/

Package Destination: /usr/lib/python2.4/site-packages/

Test package example:

Installation

Installing the Package Tools — The Hitchhiker's Guide to Packaging v1.0 documentation - http://guide.python-distribute.org/installation.html

  • Pip Installs Python (Pip)
Pip is an installer for Python packages written by Ian Bicking. It can install packages, list installed packages, upgrade packages and uninstall packages. The pip application is a replacement for easy_install. It uses mostly the same techniques for finding packages, so packages that were made easy_installable should be pip-installable as well.

Dependency Installation:

# dependency setuptools
# Python Package Index : setuptools 0.6c11 - http://pypi.python.org/pypi/setuptools
# source code: http://pypi.python.org/packages/source/s/setuptools/
# VER=1.1.6
VER=2.0
mkdir -p ~/.src ; cd ~/.src
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-$VER.tar.gz
tar -zvxf setuptools-$VER.tar.gz
cd setuptools-$VER
sudo python setup.py install
# yum method, if available
yum install python-setuptools

Easy Install:

sudo easy_install pip

Windows:

C:\python27\scripts\easy_install.exe pip

Apt:

sudo apt-get install python-setuptools
sudo easy_install pip

Manual Installation:

PIP_VER=1.4.1
mkdir -p ~/.src ; cd ~/.src
# Python Package Index : pip 1.0.1 - http://pypi.python.org/pypi/pip#downloads
wget http://pypi.python.org/packages/source/p/pip/pip-$PIP_VER.tar.gz
tar -zvxf pip-$PIP_VER.tar.gz
cd pip-$PIP_VER 
sudo python setup.py install
# fix pip:
sudo sed -i 's%#!/usr/bin/python%#!/usr/bin/env python%' /opt/python26/bin/pip

NOTE: Most packages failed during installation. The simple 'test' package installed though.

Packages are generally installed into:

/usr/local/lib/python2.7/dist-packages/

PyPI

Create PIP Package

setup.py: [1]

from distutils.core import setup

setup(
    name='kentest',
    version='0.1.0',
    author='Kenneth Burgener',
    author_email='kenneth@oeey.com',
    packages=['kentest'],
    url='http://pypi.python.org/pypi/kentest/',
    license='LICENSE.txt',
    description='kentest.',
    long_description=open('README.txt').read(),
)

Create distribution package:

# output to: dist/[PROJECT]-[VERSION].tar.gz
python setup.py sdist

Test install:

sudo pip install dist/[PROJECT]-[VERSION].tar.gz

Remove Test install:

sudo pip uninstall [PROJECT]

-- New Upload Process --

Install Twine:

python3 -m pip install --upgrade twine

Upload to test repository:

python3 -m twine upload --repository testpypi dist/*

Upload to main repository:

python3 -m twine upload dist/*

ref: https://packaging.python.org/tutorials/packaging-projects/

-- Old Upload Process --

Configure PyPI settings:

# ~/.pypirc
python setup.py register

Upload project:

python setup.py sdist upload

-- Install --

Install project:

pip install kentest

---

Sections:

Scripts:

scripts=['bin/mytool']

Data files: ("*" doesn't appear to work)

     data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
                 ('config', ['cfg/data.cfg']),
                 ('/etc/init.d', ['init-script'])]

Package files:

     packages=['mypkg'],
     package_dir={'mypkg': 'src/mypkg'},
     package_data={'mypkg': ['data/*.dat']},

References:

---

reStructuredText

PyPI uses reStructuredText instead of Markdown (shame!)

Install pypandoc:

yum install pandoc
pip install pypandoc

To have setup.py auto convert it, install pandoc and add the following: [2]

from setuptools import setup
try:
    from pypandoc import convert
    read_md = lambda f: convert(f, 'rst')
except ImportError:
    print("warning: pypandoc module not found, could not convert Markdown to RST")
    read_md = lambda f: open(f, 'r').read()

setup(
    # name, version, ...
    long_description=read_md('README.md'),
    install_requires=[]
)
This will automatically convert README.md to RST for the long description using on PyPi. When pypandoc is not available, then it just reads README.md without the conversion – to not force others to install pypandoc when they wanna just build the module, not upload to PyPi.
So you can write in Markdown as usual and don’t care about RST mess anymore. :-)

rst test tool: http://rst.ninjs.org/

References: