Skip to content

JefvdA/Pypi-package-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pypi package template

This is a template for pypi packages.

Default this comes with a MIT licence.

Before you start

Add your package's source code to the "src" directory.

Make sure to change your project's info in the setup file!

What's included?

  • tests module (excluded from package builds)
  • Makefile with some simple commands

Building your package

To build your package you will need to install build with pip.

Install pip:

Windows:

Download the get-pip.py file, then execute this python script, which will install pip for you.

Linux (apt):

For python3:

sudo apt update
sudo apt install python3-pip

For python2:

sudo apt update
sudo apt install python-pip

Install build:

Now that pip has been installed, we can use it to install build:

pip install build

Now that build is installed, we can build our package:

python3 -m build

This will generate a source archive (tar.gz) and a build distribution (.whl) in the directory "dist". These are the files we want to upload to the registery.

Publishing your package

To upload our package to the pypi registery, we will need twine:

pip install twine

Now that twine is installed, we can finally publish our package.

Follow the next steps for uploading your package to PyPi, but it might be smart to try TestPyPi first, as this is a registery like PyPi made for testing and experimenting.

PyPi

As PyPi is twine's default registery, it's a pretty simple command that we need:

python3 -m twine upload dist/*

This command will ask for your credentials (username and password), you need to log in before uploading. You can create an account here.

Click here to see how you can install and use your package in another python project.

TestPyPi

As TestPyPi is not twine's default registery, we need to specify the registery we want to use in the command:

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

This command will ask for your credentials (username and password), you need to log in before uploading. Be aware, TestPyPi uses it's own servers, and you need a different account then for PyPi. You can create a TestPyPi account here.

Click here to see how you can install and use your package in another python project.

Importing your package

Now that your package is uploaded to the python registery, you can install it with pip, and use it in other python projects.

Install from PyPi

As PyPi is pip's default registery, we don't need to specify it, use following command to install a pip package (ofcourse, change [package name] to the name of your own package):

pip install [package name]

Install from TestPyPi

As TestPyPi is not pip's default registery, we need to specify that we're using it, use following command to install a pip package (ofcourse, change [package name] to the name of your own package):

pip install -i https://test.pypi.org/simple/ [package name]

Importing the package

Now that we have successfully installed our pip package, we can move on.

If you are not sure, you can get a list of all currently installed pip packages with following command:

pip list

Now you can import the package in another python module like this:

from example_package import example

example.add_one(4) # This will return 5

This example package is used as example on how to import above

Be aware, after from doesn't come the name of the package, it is the name of a module specified in the src directory.

Good luck

I don't know who will ever read this, but if you are reading this now, thank you!

I wish you good luck on your next coding adventures, and hope you will one day create an amazing pip package!

@2022 Jef van der Avoirt

About

A template to create a python package

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published