Last modified: May 28, 2025 By Alexander Williams
How to Install Python Package with setup.py
Installing Python packages using setup.py
is a common method. It helps manage dependencies and install packages locally. This guide will walk you through the process.
Table Of Contents
What is setup.py?
setup.py
is a Python script used to install packages. It contains metadata about the package. It also defines dependencies and installation instructions.
This file is part of the setuptools library. It simplifies package distribution and installation.
Prerequisites
Before installing, ensure you have Python and pip installed. Check their versions using these commands:
python --version
pip --version
If pip is missing, install it first. You can also use requirements.txt for bulk installations.
Step 1: Download the Package
First, download the package containing setup.py
. You can get it from GitHub or PyPI. Alternatively, use a local directory.
git clone https://github.com/example/package.git
cd package
Step 2: Install the Package
Navigate to the directory containing setup.py
. Run the following command to install:
pip install .
This command reads setup.py
and installs the package. It also installs any dependencies listed in the file.
Step 3: Verify Installation
After installation, verify the package is installed correctly. Use the following command:
pip show package-name
Replace package-name with the actual package name. This will display package details.
Common Issues and Fixes
Sometimes, installation may fail due to missing dependencies. Ensure all dependencies are installed beforehand.
If you encounter permission errors, use the --user
flag:
pip install --user .
For more complex setups, refer to the package documentation. Some packages may require additional steps.
Example: Installing a Sample Package
Let’s walk through an example. Assume we have a package with this setup.py
:
from setuptools import setup
setup(
name="example_package",
version="0.1",
packages=["example_package"],
install_requires=[
"requests>=2.25.1",
],
)
This script defines a package named example_package. It requires the requests
library.
To install it, run:
pip install .
The output should confirm successful installation:
Successfully installed example_package-0.1 requests-2.25.1
Conclusion
Installing Python packages with setup.py
is straightforward. It ensures all dependencies are met. This method is useful for local development.
For more advanced setups, explore other tools like Tkinter or Paramiko. Happy coding!