DEV Community

Cover image for How to Publish and Use Your Python Package in  and from Private Bitbucket or Github

How to Publish and Use Your Python Package in and from Private Bitbucket or Github

I am a Staff Software Engineer in machine learning
・4 min read

Python has become one of the most popular programming languages. One major reason is that we, regular Python users, are free to share our code and others can use it very conveniently. A formal way of such code sharing is to pack all your code into a package and upload it to the Python Package Index (, through which other Python users can install your package easily using the pip tool.

If you have published a Python package yourself, you should know that the process is not difficult. However, for those who have never done it, you may have mistakenly thought that it must be a painful process.

In this article, I’ll show you the steps to preparing your package and publishing it.
But I will do it by uploading to bitbucket, not in Because If you are doing it for a company they may not want to upload it for public use. So let's get into it.

Apparently, the first step is to complete your project. However, we all understand that packages are never perfect. As a result, you don’t have to wait for the completion of your project before you can figure out how to publish your package. The setup file
As mentioned previously, we use the file to indicate how our package should be prepared. The following code shows you what a typical file looks like:

For the purpose of the current tutorial, let’s suppose that the directory that has all your Python files (e.g., as shown later) is called you_package. This is the one that you want to publish. To keep things simple, the file has just the following code, which will be used when we’re ready to import the package after its publication:

def hello_world():
    print("Hello World!")
Enter fullscreen mode Exit fullscreen mode

1. Prepare Files

Before we dive into the details for publishing, let’s first structure the directory with the necessary support files:

|-- your_package

Enter fullscreen mode Exit fullscreen mode
  • At the root directory, at the same level as the your_package, create the file. This file will cover things that you want the users to know about your package, such as installation and use instructions.

  • Another file at the same level is the file. This file will contain the information necessary to set up the package for publishing. We’ll talk about what’s inside this file in a minute.

  • Within the your_package directory, create the file. This file has to be named such, and what it does is convert a regular directory to a Python package. For the current simple package, we can just put the code in the for exporting

from you_package.file_0 import hello_world

The README file

Theoretically, you can use just plain text for this file, but I prefer to use a markdown file that gives you formatting options to make it more interesting to read.
Please note that the gist is saved as a TXT file, which shows you the source text for the markdown before bitbucket automatically applies the formatting for markdown files.
Usually, it’s recommended that you include the license information about the package so that the users can know the terms.

The setup file

As mentioned previously, we use the '' file to indicate how our package should be prepared. The following code shows you what a typical '' file looks like:

import setuptools

with open("") as file:
    read_me_description =

    author="Your Name",
    description="This is a test package.",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
Enter fullscreen mode Exit fullscreen mode
  • We first import the setup tools module, which has convenient methods for packaging setup.
  • In the setup() function call, most parameters are very straightforward and should be self-explanatory. Some highlights are shown below:

name: The distribution name of the package, which has to be unique on To ensure uniqueness, it’s always a good idea that you append your PyPI username to your package name if you want to upload it in Pypi also.

long_description: The long description is usually set from the file. To show the description properly, you specify that it is a markdown file by setting the long_description_content_type parameter.

packages: The list of packages that you want to publish in the distribution package. Because we’re only publishing just one package (your_package), its name is shown in a list. However, if you want to publish all packages in the directory, you can use setup tools.find_packages() to retrieve them conveniently.

python_requires: Specifies the version of Python that your package requires.

When you use it from another codebase pip will take the version name you mentioned in the file.

Now I am assuming you have committed and pushed this code into Bitbucket. Now it is time to use it from another codebase.

from the root directory, you want to use this library
pip install git+

or if you want to download a specific tagged version
pip install git+

It will download the version you have in the file, actually the latest version.

If you want to download a specific version tagged in 'git tag', you can tag that commit using git and update that version in the file also. Because pip does not recognize git tag. Pip download that Commit you have tagged and treat the version written in as the library version.


In this article, we learned the major steps that we use to create a Python package for distribution through bitbucket.


Discussion (0)