Python Apps: Requirements, Freezing Dependencies, and .gitignore

When sharing or deploying a Python app, it's essential to manage dependencies and ensure that sensitive or unnecessary files aren't included in the repository. This guide covers how to:

  • Create a requirements.txt file
  • Freeze your app dependencies
  • Use .gitignore to exclude unnecessary files
  • Best practices for sharing your Python app

Creating a requirements.txt File

A requirements.txt file is used to list all of the Python packages your app depends on. This file allows others to easily recreate the environment by installing all required packages.

Creating the requirements.txt

To create a requirements.txt file, you can use the following command:

pip freeze > requirements.txt

This command will capture the current environment's installed packages and their versions, and output them into a file named requirements.txt. You can then share this file with others.


Freezing Dependencies

Freezing dependencies is an essential part of managing Python apps, especially for sharing or deployment. Freezing ensures that the specific versions of the packages used in your app are preserved, which helps maintain compatibility and prevents issues when others set up the app.

Freezing Your App’s Dependencies

To freeze the current installed packages in your virtual environment, use:

pip freeze > requirements.txt

This will record the exact versions of all installed packages. For example, the output might look like this:

Flask==2.0.1
requests==2.26.0
pandas==1.3.3

This makes it easy for anyone else working on the app to install the same versions.


Using .gitignore in Your Python App

When sharing a Python app on Git, you need to exclude certain files and directories that aren't necessary for the app, such as virtual environment files, compiled Python files, or IDE-specific files.

Typical Entries for .gitignore

Create a .gitignore file in the root of your app and include the following common patterns:

# Ignore Python bytecode
*.pyc
*.pyo
__pycache__/

# Ignore virtual environments
venv/
env/
ENV/

# Ignore IDE files (e.g., VS Code, PyCharm)
.vscode/
.idea/

# Ignore environment-specific files (e.g., macOS, Windows)
.DS_Store
Thumbs.db

# Ignore logs and temporary files
*.log
*.tmp

# Ignore configuration files specific to environments
*.env

This ensures that sensitive or unnecessary files are not included in your Git repository.


How to Share Your Python App

When sharing a Python app, it's important to ensure that others can easily recreate your development environment. Here are steps to prepare your app for sharing:

Steps to Share Your Python App

  1. Create a Virtual Environment: Ensure that your app is set up with a virtual environment. This isolates your app's dependencies and avoids conflicts with system-wide packages.

    python -m venv venv
    
  2. Create and Update requirements.txt: After installing all required packages, freeze your dependencies into a requirements.txt file:

    pip freeze > requirements.txt
    
  3. Add .gitignore: Create a .gitignore file and add all the necessary files and directories to exclude (like __pycache__, virtual environment, etc.).

  4. Test the Setup: It's a good idea to test the setup in a fresh virtual environment. Clone your app and run the following commands to ensure it works correctly:

    git clone <your-app-repository>
    cd <your-app-directory>
    python -m venv venv
    source venv/bin/activate  # On Windows use venv\Scripts\activate
    pip install -r requirements.txt
    

    This ensures that all dependencies are installed correctly in the fresh environment.

  5. Document the Setup: Include a README.md file with clear instructions on how to set up and run your app, including:

    • How to create a virtual environment
    • How to install dependencies
    • Any specific configuration or environment variables required to run the app

Things to Remove from the Python App Before Sharing

Certain files and directories should be excluded from the app before sharing it, as they are either unnecessary or potentially sensitive.

Files to Remove:

  1. Compiled Python Files: These are automatically generated and don't need to be included in the repository. They are typically stored in the __pycache__ directory and have .pyc or .pyo extensions.

  2. Virtual Environment Directories: These directories (venv/, env/, ENV/) contain the installed dependencies for your app and should not be shared. Use the requirements.txt file to share the required dependencies instead.

  3. IDE or Editor Specific Files: Files such as .vscode/ or .idea/ contain app-specific settings for your IDE (like VS Code or PyCharm) and don't need to be shared with others.

  4. Operating System Specific Files: Files like .DS_Store (macOS) or Thumbs.db (Windows) are automatically generated by the operating system and can be safely ignored.

  5. Environment Configuration Files: Files containing sensitive information (like .env or .env.local files) should never be included in the repository. Use environment variables to manage configurations securely.


Best Practices for Sharing Python Apps

  • Use a Virtual Environment: Always create a virtual environment to manage dependencies and avoid conflicts with system packages.
  • Document the Setup: Include a README.md file with clear instructions on how to set up the environment and run the app.
  • Use Version Control: Always use Git for version control to track changes and collaborate with others.
  • Test on a Fresh Setup: Before sharing your app, test it by setting it up from scratch in a new virtual environment to ensure it works as expected.

Info

When sharing a Python app, ensure sensitive information (such as API keys or credentials) is not included in the repository. Use environment variables or external configuration files for sensitive data.


By following these practices, you ensure that your Python app is well-prepared for sharing and that others can easily set it up and contribute without any issues.

Copyright © 2025 Devship. All rights reserved.

Made by imParth