With Python distributions such as Enthought and Anaconda, its never been easier to create and install python packages. In this post I show you how to build your own python packages and publish them on the PyPI python package index.
Step 1. Create PyPI Account
Before we can upload our package to the Python Package Index PyPI we must first create an account.
Go to the PyPI website and set up an account. Remember your user name and password as you will need these later to upload your files.
Step 2. Prepare your files
Before we build and upload your module, we need to organise our files in a particular way.
For this basic example we’re assuming your project code is in a single python file “module_file.py”. You will also need to create a file “setup.py” and “__init__.py”.
Create the folder structure like this:
project_directory/ setup.py module_name/ __init__.py module_file.py
Everything in the directory ‘module_name’ will be packaged and uploaded to PyPI.
The file “__init.py__” tells Python that the folder it is in is a module folder. For now we’ll just leave the file blank.
The file “setup.py” is a setup script that defines important information and metadata for your project.
For our simple example we’ll just include some basic information about the package and its author:
from setuptools import setup setup(name='texlog', version='0.2.0', description='Log the wordcount of tex files', url='https://github.com/deparkes/texlog', author='deparkes', firstname.lastname@example.org', license='GPL', packages=['texlog'])
Step 3. Build and Upload Your Package To PyPI
Having sorted out your project files and organisation, the next step is to build your package and upload it to PyPi.
We do this by running a series of commands from ‘project_directory’.
1. setup.py register
Running the command
registers your project on PyPI. Running this command will give you these options:
running register We need to know who you are, so please choose either: 1. use your existing login, 2. register as a new user, 3. have the server generate a new password for you (and email it to you), or 4. quit Your selection [default 1]:
At which point you can login using your PyPI username and password.
You will be given the option to save your username and password as a plaintext file on your computer. Since this is just an unsecure plaintext file you may wish to not do this – it just means you’ll have to enter your login details each time you register or upload a package.
2. setup.py sdist
This is the step that actually builds your package. For now the default options will be enough, which should create a zip-file package in a directory “dist”
sdist refers to a “source distribution”. You can find out more about different distribution types here.
3. twine upload dist\*
Now we get to the stage of uploading our package to PyPI.
To upload all of the packaged files in the directory ‘dist’ run:
twine upload dist\*
If you didn’t save your username and password for PyPI when you ran “setup.py register” you’ll need to use the username and password flags:
twine -u USERNAME -p PASSWORD upload dist\*
Note: If you don’t have twine you’ll need to run
pip install twine
Install your package
With your project packaged and uploaded to PyPi you and others can now easily install it using the “pip install” command:
pip install jobid