DEV Community

Ajmal Hasan
Ajmal Hasan

Posted on

Setting Up a Conda Virtual Environment for Your Python Projects - 1

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
Enter fullscreen mode Exit fullscreen mode

c) Also disable default environment activation using(optional):

conda config --set auto_activate_base false
Enter fullscreen mode Exit fullscreen mode

d) Also add pip path:

echo "alias pip=$(which pip3)" >> ~/.zshrc && source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

2. Create a Project Folder and Virtual Environment

a) Navigate to your project directory:

   mkdir my_project && cd my_project
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If you encounter errors when running, remove any broken or partially created environment:

   conda remove --name venv --all
Enter fullscreen mode Exit fullscreen mode

c) Activate the virtual environment:

   conda activate venv
Enter fullscreen mode Exit fullscreen mode

You can check all active enviroments using:

conda env list      
Enter fullscreen mode Exit fullscreen mode

d) To deactivate the environment:

   conda deactivate
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

or

python3 -m pip install ipykernel langchain
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

b) Install Dependencies from requirements.txt

To recreate the same environment in another system or environment:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Image description

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)