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
c) Disable the default environment activation (optional):
conda config --set auto_activate_base false
d) Add the pip3
path for convenience (optional):
echo "alias pip=$(which pip3)" >> ~/.zshrc && source ~/.zshrc
2. Create a Project Folder and Virtual Environment
a) Create and navigate to your project directory:
mkdir my_project && cd my_project
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
Check your Python version using:
python --version
If you encounter errors, remove any broken or partially created environments:
conda remove --name venv --all
c) Activate the virtual environment:
# For a directory-specific environment
conda activate ./venv
# For a central environment
conda activate venv
d) To deactivate the environment:
conda deactivate
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
- Or generate the file automatically:
pip freeze > requirements.txt
b) Install Dependencies from requirements.txt
Install all libraries listed in the file:
pip install -r requirements.txt
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
List all installed dependencies with:
conda list
5. Install Additional Dependencies (Optional)
For example, to install Jupyter Kernel:
conda install ipykernel -y
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/
└── ...
To run Python scripts:
python app.py
To run Streamlit applications:
streamlit run app.py
With this setup, you can efficiently manage Python projects using Conda environments. Happy coding!
Top comments (0)