Create a Python package?

We assume that we are creating a package shortpath.

The package should contain these files.
README.rst
MANIFEST.in
setup.py
setup.cfg (If you have any configuration to your package)

Now we have to build our setup.py based on our package details, for creating setup.py follow this.

If you are done with the above steps, your package structure will be something like

/shortpath

shortpath/

__init__.py
shortpath.py

MANIFEST.in
README.rst
__init__.py
setup.py

The shortpath.py will contain your methods, classes. Now, you can test your package locally using

$ pip install -e .

>>> import shortpath
>>> print some_method()

Now for uploading your package to PyPI,

$ pip install twine

Follow the link for registering on PyPI and configuring .pypirc

To generate your package distribution

$ python setup.py sdist

To generate you package info

$ python setup.py egg_info

Now upload your package with command

$ twine upload dist/*

Install the uploaded package

$ pip install shortpath

Note: If any error in installing the package it must be conflicting with the local package so remove the local package and symlink

  • The packages you will find in
    /usr/local/lib/python/dist-packages/
  • The symlink will be in
    /usr/local/lib/python/dist-packages/easy-install.pth
Advertisements

One thought on “Create a Python package?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s