Title: Setting Up Streamlit Project with pip and venv

  • Understanding Streamlit
  • Setting up Streamlit Project with pip and venv
  • Working with Streamlit
  • Debugging Streamlit Apps in VS Code
The virtual environment described in this note is applicable to other Python projects as well, not just limited to Streamlit application development.



Why pip and venv are Essential for Your Streamlit App (Python Projects)

pip and venv are tools essential practices for setting up your Streamlit app project.

  • ✦ They ensure that your development process is smooth, your app is portable, and your project environment is reproducible.

  • ✦ Being able to understand and able to apply these tools means taking a significant step towards building robust, shareable web applications.

  • ✦ Dependency Management with pip:

    • Streamlit apps, like any other Python-based project, rely on specific packages and frameworks
      • We use packages such as LangChain, OpenAI, ragas and many others
    • pip allows you to manage these dependencies efficiently.
      • By using pip to install and manage necessary packages, you ensure that your app has all the right tools to run smoothly.
      • 🔥Moreover, pip helps in specifying the exact versions of these packages, preventing potential conflicts that might arise from updates or incompatibilities.
  • ✦ Isolated Environments with venv:

    • Developing your Streamlit app within a virtual environment created by venv is a best practice that offers several benefits.
    • It isolates your project's dependencies from the global Python environment, ensuring that your app doesn't interfere with or get affected by other Python projects on the same system.
    • This isolation is particularly important when you're working on multiple projects with differing requirements.
    • It also simplifies the process of sharing your app with others or deploying it, as you can easily specify the environment's requirements.
  • ✦ Reproducibility and Collaboration:

    • When working on a Streamlit app, especially in a team or for public release, reproducibility becomes paramount.
    • By using venv and pip together, you can create a requirements.txt file that lists all your project's dependencies.
    • This file ensures that anyone who wants to run your app can set up an identical environment with ease, making collaboration and sharing straightforward.



pip

pip is the package installer for Python.

  • ✦ You can use it to install packages from the Python Package Index (PyPI) and other indexes.
  • ✦ It allows you to install, update, and remove Python packages. Using pip ensures that you can easily share and manage dependencies for your projects.

Why Use pip?

  • ✦ Dependency Management: Easily manage project dependencies.
  • ✦ Consistency: Ensure that you're using the same package versions across different environments.
  • ✦ Ease of Use: Simplify the installation process for Python packages.

pip is included by default with Python versions 3.4 and later. You can check if pip is installed by running: pip --version

Basic pip Commands

  • ✦ Installing Packagespip install package_name
  • ✦ Uninstalling Packagespip uninstall package_name
  • ✦ Listing Installed Packagespip list
  • ✦ Searching Packagespip search package_name
  • ✦ Upgrading Packagespip install --upgrade package_name



venv

venv is a module that comes pre-installed with Python 3.3 and later versions, used to create isolated Python environments. Each environment has its own installation directories and doesn’t share libraries with other environments.


Why Use venv?

  • ✦ Isolation: Keep dependencies required by different projects separate.
  • ✦ Control: Have control over your environment and dependencies.
  • ✦ Testing: Test your projects in clean environments to ensure compatibility.

Creating a Virtual Environment

To create a virtual environment, you can use the following command:

python -m venv myenv

This command creates a directory called myenv in your current directory, containing a fresh, isolated Python environment.


Activating the Virtual Environment

  • ✦ On Windows
    • myenv\Scripts\activate.bat
  • ✦ On Unix or MacOS
    • source myenv/bin/activate

Managing Packages Within a Virtual Environment

While the virtual environment is activated, you can use pip to install, update, or remove packages. These operations will only affect the current virtual environment.

How it works

  • ✦ Once you activate a virtual environment created by venv, it essentially sets up a secluded sandbox for your Python project.
  • ✦ In this isolated environment, any commands you execute using Python or pip are confined to this sandbox.
  • ✦ This means that if you install, update, or remove Python packages using pip, these actions will only affect the virtual environment and not the global Python installation on your system.
  • ✦ Similarly, running Python scripts will use the Python interpreter and any dependencies within this environment, ensuring that your project runs under the conditions you've explicitly set up.
  • ✦ This isolation is crucial for managing project-specific dependencies without risking conflicts with other projects or the system-wide Python setup, thereby providing a consistent and controlled development and execution environment.

Deactivating the Virtual Environment

To deactivate the virtual environment and use your global Python environment again, simply run:
deactivate



Walkthrough on Setting up the Environment for Streamlit Project

Major Timeline
  • 00:00 - Setting up the Virtual Environment (using venv)
  • 05:55 - Activate the Virtual Environment
  • 08:11 - Install packages within the newly created Virtual Environment (using pip)
  • 11:40 - Export the "versions numbers" of the installed packages (using pip)
  • 14:47 - Install packages from a requirements.txt (using pip)

Suggest to watch @ 1.25x speed and watch it directly on YouTube if you want to able to resize the video windows.

Here is some common packages that give a good starting point for the virtual environment

pip install streamlit openai tiktoken python-dotenv langchain langchain-openai langchain-experimental pandas

Alternatively, you can use this requirements.txt as the starting point.