Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Packages and Setup

We have seen how you can write your own libraries and packages of code and import them when they are in the same folder as a script. What if you want to be able to import your code from any location by installing it to your Python packages?

import mypack

You can do all this using a file called setup.py and the setuptools Python package.

Package Structure

There is a bit of variation in how Python packages are structured in practice. We’ll talk about one option, which is fairly standard for low-complexity projects. Let’s say your Python package is called mypack. All the source materials for your package (including code, documentation, tests...) should be contained in a single folder, ideally a git repository. One example is the package in the python-packages repository.

Inside this top-level folder, you will have several sub-folders and files:

There may be additional files as well (e.g. .git, files for CI, etc.) the above list is not exhaustive.

requirements.txt

This is a file which lists dependencies of your package. You can list packages without version numbers, or use some version specifiers

matplotlib # no version number
numpy >= 1.18 # must be version 1.18 or later
scipy >= 1.5 # must be version 1.5 or later

You can install requirements using conda:

conda install --file requirements.txt

or pip:

pip install -r requirements.txt

setup.py

This is a file that is used to configure and install your repository to your Python path. From the root of your repository, you should be able to run

pip install .

to install the code to your Python path. You can then use mypack anywhere - not just in the directory that contains the source code.

In order to uninstall your package, you can run

pip uninstall mypack

You can also run setup.py as a script:

python setup.py install

In general, you’ll want to use the setuptools package. Basic useage is to use setuptools.setup, which accepts a variety of keyword arguments that help build and describe your package. See here for more details.