DEV Community

Ajmal Hasan
Ajmal Hasan

Posted on • Edited on

12

Setting Up Virtual environment in Python Projects with Conda - 1

Creating isolated environments is crucial for managing dependencies and avoiding conflicts in Python projects. This guide will help you install Anaconda, resolve common issues, and set up a virtual environment for your projects.


1. Install Anaconda

a) Follow this guide to install Anaconda using the installer. Ensure Anaconda is added to your shell configuration (~/.zshrc or ~/.bashrc).

b) Verify the installation by running:

   conda --version
Enter fullscreen mode Exit fullscreen mode

c) Disable the default environment activation (optional):

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

d) Add the pip3 path for convenience (optional):

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

2. Create a Project Folder and Virtual Environment

a) Create and 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 your desired version):

   # Environment created in the current directory
   conda create -p ./venv python=3.10 -y
# OR
   # Environment created in Conda's central directory
   conda create -n venv python=3.10 -y
Enter fullscreen mode Exit fullscreen mode

Check your Python version using:

   python --version
Enter fullscreen mode Exit fullscreen mode

If you encounter errors, remove any broken or partially created environments:

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

c) Activate the virtual environment:

   # For a directory-specific environment
   conda activate ./venv

   # For a central environment
   conda activate venv
Enter fullscreen mode Exit fullscreen mode

d) To deactivate the environment:

   conda deactivate
Enter fullscreen mode Exit fullscreen mode

3. Why Use Virtual Environments?

  • Isolation: Keeps dependencies separate for each project.
  • Consistency: Ensures identical environments across systems.
  • Reproducibility: Facilitates sharing and replication of the project setup.

4. Manage Dependencies with requirements.txt

Tracking dependencies ensures collaboration and smooth deployment.

a) Save Dependencies to requirements.txt

  • Manually list the required libraries:
  langchain
  openai
  python-dotenv
  streamlit
Enter fullscreen mode Exit fullscreen mode
  • Or generate the file automatically:
  pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

b) Install Dependencies from requirements.txt

Install all libraries listed in the file:

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

To add new libraries, update requirements.txt and re-run the command.

c) Remove Dependencies

Uninstall all libraries listed in the file:

pip uninstall -r requirements.txt -y
Enter fullscreen mode Exit fullscreen mode

List all installed dependencies with:

conda list
Enter fullscreen mode Exit fullscreen mode

5. Install Additional Dependencies (Optional)

For example, to install Jupyter Kernel:

conda install ipykernel -y
Enter fullscreen mode Exit fullscreen mode

6. Finalize the Project Structure

Manually create the necessary files and organize the folder structure as per your project’s requirements. A typical structure may look like this:

my_project/
│
├── app.py
├── requirements.txt
├── venv/
└── ...
Enter fullscreen mode Exit fullscreen mode

To run Python scripts:

python app.py
Enter fullscreen mode Exit fullscreen mode

To run Streamlit applications:

streamlit run app.py
Enter fullscreen mode Exit fullscreen mode

With this setup, you can efficiently manage Python projects using Conda environments. Happy coding!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more