Tuesday, 15 April 2025

 

How to Set Up a Python Virtual Environment and Get Project-Specific pip list

If you're working on a Python project and want to isolate your dependencies — and generate a list of installed packages only for that project — follow this simple guide. This is especially helpful if you have .py files in a folder (like app.py, travel_engine.py) and want a clean setup.


๐Ÿ“ Project Folder Example

Let’s say your project is located at:


C:\Users\sek\Downloads\travelbuddy\

✅ Step 1: Open Command Prompt

  1. Press Win + R, type cmd, and press Enter.

  2. Navigate to your project folder:


cd C:\Users\sek\Downloads\travelbuddy\

✅ Step 2: Create a Virtual Environment

python -m venv venv

This creates a venv/ folder with an isolated Python environment.


✅ Step 3: Activate the Virtual Environment

venv\Scripts\activate

Once activated, your terminal will show the environment name like this:

(venv) C:\Users\sekha\Downloads\travelbuddy_flask-main\travelbuddy_flask-main>

✅ Step 4: Install pipreqs (to auto-detect dependencies)

pip install pipreqs

✅ Step 5: Generate requirements.txt

pipreqs . --force

This scans your .py files (like app.py, travel_engine.py) and writes a requirements.txt file with only the third-party libraries actually used.


✅ Step 6: Install Required Packages

pip install -r requirements.txt

This installs everything your project needs inside the virtual environment.


✅ Step 7: List Installed Packages (Project-Specific)

pip list

This gives you a clean list of packages installed only in this environment, not globally.


✅ (Optional) Freeze Package Versions

If you want an exact versioned list (great for sharing or deployment):

bash

pip freeze > full_requirements.txt

๐Ÿงน Tip: To Deactivate the Virtual Environment

Just run:

bash

deactivate

๐Ÿง  Final Thoughts

Using a virtual environment keeps your project clean and avoids "dependency hell" with conflicting packages. Tools like pipreqs make it even easier by auto-generating requirements.txt from your code.

Let me know if you’d like to automate this setup even more or troubleshoot anything!

4 comments:

  1. Linux networking tips boost real-time performance just like rsl helper enhances real-time efficiency for users.

    ReplyDelete
  2. Thanks for sharing this article.

    Learn Python, Power BI, SQL, Tableau and Jira with the best trainers by joining a guided data analyst course in hyderabad and also get placement opportunities

    ReplyDelete

  3. Excellent guide. The distinction between workflows and projects is a standout feature here—it’s a nuance that’s often missed but is vital for real-world application. Truly an intuitive deep dive into the Linux lifecycle!

    ReplyDelete
  4. Nice post! The details provided are easy to understand and valuable. If anyone is looking to upgrade their skills, Velgrow Academy Chennai is worth exploring. Helpful information about best guest posting sites.

    ReplyDelete