Skip to content

Instantly share code, notes, and snippets.

@luabida
Last active November 23, 2022 18:22
Show Gist options
  • Save luabida/9d7c013b1a1d94cd2477c1de7adcb0cd to your computer and use it in GitHub Desktop.
Save luabida/9d7c013b1a1d94cd2477c1de7adcb0cd to your computer and use it in GitHub Desktop.
Packages

Installing Python packages and managing Virtual Environments

How to install a package?

# https://packaging.python.org/en/latest/tutorials/installing-packages/
$ python3 -m pip --version
$ python3 -m ensurepip --default-pip
# https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/

$ pip install -r requirements.txt                               # from a file
$ pip install -e git+https://git.repo/some_pkg.git              # from git
$ pip install -e git+https://git.repo/some_pkg.git@feature      # from a branch
    
"""Using virtual environments allows you to avoid 
installing Python packages globally which could 
break system tools or other projects."""

$ python3 -m pip install ttrack # py2 project

How to avoid dependencies incompatibility using Mamba

# https://github.com/conda-forge/miniforge

$ curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh"
$ bash Mambaforge-$(uname)-$(uname -m).sh

How to use Python 2.7 packages

$ mamba create --name "py27" python=2.7 -y
$ mamba env list
$ mamba activate py27
$ mamba list
$ pip --version
$ pip install ttrack

Where Packages are stored

# Every package (code) has a home 🏡

import os
import inspect
import setuptools

os.path.abspath(inspect.getfile(setuptools))

Trying setuptools

# https://setuptools.pypa.io/en/latest/userguide/quickstart.html#setup-py
from setuptools import find_packages

setup(
    name='my_package',
    version='0.0.1',
    packages=find_packages(
        where='src',
        include=['mypackage*'],
        exclude=['mypackage.tests'],
    ),
)

$ python3 -m pip install -e .

Trying Poetry

# https://python-poetry.org/docs/
$ curl -sSL https://install.python-poetry.org | python3 -
$ mkdir my_package
$ poetry new my_package
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment