DEV Community

loading...
Cover image for How to Python Package 📦 - Setting up

How to Python Package 📦 - Setting up

mbnlc profile image mbnlc Updated on ・3 min read

In my previous post I mentioned that this first 'real' post would give you an understanding of how to set up an environment to build your own python package. However, rather than focusing on the technical aspects (there are always multiple approaches), I want to particularly emphasise the decisions I made when building yflive and the reasoning behind them.

Please keep in mind, that I am not an expert on this topic. I am simply sharing my experience.

If you haven't read the first article, I encourage you to check it out. It is, however, not necessary to understand the following.


Setting up a new project

I am going to assume you have python installed. I will not be working with conda.

A python package has the same structure as any python project, with a couple of key differences, which we will focus on when it comes to packaging the final version.

To get started simply create a new empty folder/directory.

Virtual Environments

As with almost any python project, my initial step is to create a virtual environment in my empty project directory. This is common practice to separate project dependencies from globally installed modules and packages. It helped me keep the project clean, but especially benefited my personal system as there was no need to install packages globally.

If you aren't familiar with virtual environments, you might want to check out this article which gives a detailed explanation.

To create a virtual environment you want to cd to your project directory and run the following command:

python3 -m venv ./venv
Enter fullscreen mode Exit fullscreen mode

This will create a new sub-folder in your project directory.

Before interacting with your new virtual environment, you will have to activate it. On MacOS or Unix this can be achieved by running the following command:

source ./venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

For Windows it's slightly different:

venv\Scripts\activate.bat
Enter fullscreen mode Exit fullscreen mode

You will be installing any dependencies for your own package into this virtual environment.

After you've finished using the virtual environment, you can exit it by simply running:

deactivate
Enter fullscreen mode Exit fullscreen mode

I found these videos particularly helpful, while learning about virtual environments.


Essential project files/directories

Every python project should contain some essential files/directories. Even if it's simply for good measure (I'm looking at you, tests folder 👀)

project_dir/
  |  --> venv/               : virtual environment
  |  --> tests/              : unit tests
  |  --> my_project/         : core functionality
            --> ...
  |  --> LICENSE.txt         : software license
  |  --> README.md           : description of project
  |  --> requirements.txt    : list of dependencies
  +  --> setup.py            : executable to build project
Enter fullscreen mode Exit fullscreen mode

Above you can see a basic file tree for a new project. In this case, I'm intentionally leaving out git related files as I shall cover them in a later post.

(Personally, I also enjoy an untracked directory for messing around)

As I previously said, there are always multiple ways to do things. The above setup has worked for me, however I don't want you to fixate on this approach. Please also do your own research.

I personally set up my project like this, because it offered me a low footprint, allowing me to focus on writing the code that mattered. Once I finished the core functionality, I had the ability to fill in the missing parts without any overhead.

Getting started

At this point you're set up to write code for your python package. Don't worry about the other files for now. They shall come in handy at a later point.

In the next post I shall cover the open source side of things. Looking into licenses, git and versioning. This will be the groundwork for maintaining your package in the future.

Hope to see you there! 😃


Shameful advertisement:

If you want to check out my own python package you can find it on GitHub and Pypi. Always looking for contributors and things to improve.

Discussion (0)

pic
Editor guide