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:
β Step 1: Open Command Prompt
-
Press
Win + R
, typecmd
, and press Enter. -
Navigate to your project folder:
β Step 2: Create a Virtual Environment
This creates a venv/
folder with an isolated Python environment.
β Step 3: Activate the Virtual Environment
Once activated, your terminal will show the environment name like this:
β Step 4: Install pipreqs (to auto-detect dependencies)
β
Step 5: Generate requirements.txt
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
This installs everything your project needs inside the virtual environment.
β Step 7: List Installed Packages (Project-Specific)
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):
π§Ή Tip: To Deactivate the Virtual Environment
Just run:
π§ 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!
No comments:
Post a Comment