Setting Up Python Projects with Conda and requirements.txt
When working on Python projects, it’s essential to create isolated environments to manage dependencies and avoid conflicts. This guide will help you install Anaconda, fix common issues, and set up a virtual environment for your projects.
1. Install Anaconda (in Root Terminal)
a) Install Anaconda by following this guide(prefer: Using Installer). Ensure that you have Anaconda to your shell configuration (~/.zshrc
or ~/.bashrc
).
b) After installation, verify by running:
conda --version
c) Also disable default environment activation using(optional):
conda config --set auto_activate_base false
d) Also add pip path:
echo "alias pip=$(which pip3)" >> ~/.zshrc && source ~/.zshrc
2. Create a Project Folder and Virtual Environment
a) Navigate to your project directory:
mkdir my_project && cd my_project
b) Create a Conda virtual environment named venv
with Python 3.10(or different Python x.xx):
You can check python version using python --version
conda create -p venv python==3.10 -y
If you encounter errors when running, remove any broken or partially created environment:
conda remove --name venv --all
c) Activate the virtual environment:
conda activate venv
You can check all active enviroments using:
conda env list
d) To deactivate the environment:
conda deactivate
3. Install Libraries (Ensure Virtual Environment is Active) Or skip to next step(5)
Install libraries inside the virtual environment to keep them isolated:
pip install langchain openai python-dotenv streamlit
or
python3 -m pip install ipykernel langchain
This approach is preferred over global installation, as it avoids conflicts with other projects.
Why Use Virtual Environments?
- Isolation: Keeps project-specific dependencies separate from global installations.
- Consistency: Ensures that your project runs in the same environment across different systems.
- Reproducibility: Makes it easy to share and replicate the project setup.
4. Manage Dependencies with requirements.txt
Keeping track of your project's dependencies is crucial for easy collaboration and deployment. Here's how to do it:
a) Save Dependencies to requirements.txt
You can either:
- Manually create a
requirements.txt
file and list the libraries required for your project:
langchain_openai
langchain_core
python-dotenv
streamlit
Use pip freeze
to fetch current installed versions.
- Or automatically generate the file with all installed dependencies using
pip freeze
(if used step 4 for libraries installation):
pip freeze > requirements.txt
This command captures the exact versions of all packages installed in your virtual environment.
Example Generated by pip freeze
langchain==0.0.150
openai==0.27.2
python-dotenv==1.0.0
streamlit==1.25.0
b) Install Dependencies from requirements.txt
To recreate the same environment in another system or environment:
pip install -r requirements.txt
This ensures that all required libraries are installed with the exact versions specified in the file.
Use to list down all installed dependencies.
conda list
Why Use requirements.txt
?
- Reproducibility: Ensures that anyone working on the project installs the correct versions of dependencies.
- Portability: Makes it easy to share the environment setup with team members or deploy it to production.
- Version Control: Avoids surprises from updates or changes in package versions.
5. Final the folder structure:
You manually create rest of files as per requirements.
Also to run python use python app.py
, to run streamlit use streamlit run app.py
With this setup, you’re ready to work on Python projects efficiently using Conda virtual environments. Happy coding!
Top comments (0)