author_profile_image

mariangela

Updated on July 01, 2024

Category:

env

Tags:

Django

install private python packages

Python3

private python modules

private python libraries

virtual env

Install a private Python package

How to install a private Python package – a package not published in PyPI – in your Django project’s virtual environment

A private Python package is a package that is not published on the public Python Package Index (PyPI) and is intended for restricted use, typically within an organization, team, or individual project.

1. Python package saved on your local machine

We are going to install a private package named “basics” in the virtual environment of our Django project “mysite”.

The Python package is saved in the home directory, at the same level of the Django project.

03_home_directory

Open the Ubuntu terminal and activate the virtual environment

01_django_prj_dir 02_activate_virtual_env
cd mysite
ls -la
source env/bin/activate

Go to the dist directory of the Python package

cd ~/basics/dist
ls -la
04_private_python_package

The dist directory in a Python package is a folder where the distribution files of the package are stored after building the package.

These distribution files are what you share with others, upload to a package repository (like PyPI), or install using a package manager like pip.

There are typically two main types of distribution files you might find in the dist directory:

  • - Source Distribution or sdist (.tar.gz or .zip)
  • - Built Distribution, commonly called wheel (.whl)

A wheel is a ZIP archive. You can inspect its contents by unpacking it as a normal ZIP archive using unzip or from the command line with the Python’s zipfile module.

To list the files in the archive:

python3 -m zipfile -l basics-1.0.0-py3-none-any.whl
05_unzip_wheel_file

To extract the files from the archive in the current directory:

python3 -m zipfile -e basics-1.0.0-py3-none-any.whl ./
06_extract_files_from_wheel_archive

Inside a wheel, you will find the package’s files, plus an additional directory called package_name-version.dist-info, in this case basics-1.0.0.dist-info.

This directory contains various files, including a METADATA file which holds the project metadata, as well as a RECORD that lists all files in the wheel along with a hash of their content, as a safety check of the download’s integrity.

07_dist_info_directory

We’ll use the .whl file to install our package.

pip3 install basics-1.0.0-py3-none-any.whl
08_install_private_python_package_with_pip

Run the following command to list all the packages installed so far

pip3 freeze
09_pip_freeze

2. Python package saved in GitHub repository

Log in to GitHub from the Ubuntu terminal

gh auth login

Install your private Python package with pip

1) SSH version
pip install git+ssh://git@github.com/username/repo.git
10_install_private_github_repo 11_installing_dependencies 12_installation_success

2) HTTPS version
pip install git+https://github.com/username/repo.git