pythonimportdebian

How to import a custom package on Debian Python 3.11


Just got back to Debian Linux after years not using it. I have a very personal use custom package of tools that I want to install to use to import into my Python scripts. On Win11, I just added the package to site-packages and boom done and usable. Not so on Debian 12.11. It's not in a repo, so I can't apt-get install it. How do I go about this?


Solution

  • If you're returning to Debian and trying to use a custom Python package that isn't available via apt or PyPI, there are several clean and proper ways to make it importable system-wide or locally. Here's how to do it correctly:


    📦 Option 1: Install in Development Mode (pip install -e .)

    This is ideal if you're actively developing or modifying the package.

    Steps:

    1. Make sure python3-pip is installed:

      sudo apt update

      sudo apt install python3-pip

    2. Navigate to your package root directory (where setup.py is):

      cd /path/to/your/custom-package

    3. Install the package in editable mode:

      pip install -e .

    Now, you can freely import the package from any Python script.

    ✅ Requires a valid setup.py or pyproject.toml.


    🧱 Option 2: Install Locally to Your User (Recommended for Personal Use)

    To avoid affecting system-wide packages:

    pip install --user .

    This installs your package into ~/.local/lib/pythonX.Y/site-packages, which is already in Python’s search path.


    🌐 Option 3: System-Wide Installation (Use with Caution)

    If you're sure this package should be available globally:

    sudo pip install .

    ⚠️ Avoid unless necessary — can interfere with system tools relying on specific versions of libraries.


    🔀 Option 4: Quick Hack – Add to PYTHONPATH

    For fast testing without installation:

    export PYTHONPATH=/path/to/your/package:$PYTHONPATH

    To persist across sessions, add it to your shell config:

    echo 'export PYTHONPATH=/path/to/your/package:$PYTHONPATH' >> ~/.bashrc

    source ~/.bashrc

    ❌ Not ideal for long-term usage; better for quick tests.


    🛠 Bonus: Minimal setup.py Template

    If your package lacks a setup.py, create one at the root of your project:

    from setuptools import setup, find_packages
    setup(
        name='your_custom_package',
        version='0.1',
        packages=find_packages(),
    )
    

    Then run:

    pip install -e .


    🧪 Test That It Works

    Open a Python shell and try importing your module:

    import your_custom_package
    

    No errors? You're all set!


    🧼 Final Tips