This week, I wanted to turn a simple python script into a package and pull it into another project. Basically, I had some functionality that I wanted to isolate so why not make it a package. I’ve worked on other python packages before, but have never rolled my own so I thought this would be fun.
The python package
First up, the package file structure. After a bit of researching, a python package must contain a setup.py file and a subfolder. This stackoverflow answer was helpful for a basic package format.
After setting up my project structure, I looked into adding a very basic setup.py file. This was my starting setup.py file (based on this so question). Of course, check the docs for more details…
from setuptools import setup, find_packages
setup(name='package_name',
version='0.1',
packages = find_packages(),
)
This is the test package I created: python_people_names. This basically takes a person’s name as a string and returns a dictionary with the name split into parts (first_name, middle_name, last_name, suffix_name). It’s also a wip so no guarantees if you use it… :)
Importing the package from github
So now I have a python package on github and want to pull it into my project without submitting it to the official Python Package Index. First step, install git on my virtual machine (else it won’t work and you’ll get an error).
sudo apt-get update
sudo apt-get install git
Then pip install from github. You can add the --update flag if you make changes to your package and need the latest version of the package. In this case, I ran:
pip install --upgrade git+git://github.com/lynzt/python_people_names.git
Then to use the package in the project, import & use:
from people_names import people_namesnames = people_names.split_name('frank smith')
This article is similar to one I wrote about installing node modules from github…