How to create a new python environment
Creating a new Python environment is a great way to isolate your projects and dependencies, and ensure that you're using the right version of Python and packages for each project. Here are the steps to create a new Python environment:
Method 1: Using conda
- Install
conda
if you haven't already:pip install conda
- Create a new environment:
conda create --name myenv python=3.9
(replacemyenv
with the name you want to give your environment, andpython=3.9
with the version of Python you want to use) - Activate the environment:
conda activate myenv
- Verify that you're using the correct environment:
conda info --envs
should list your new environment
Method 2: Using virtualenv
- Install
virtualenv
if you haven't already:pip install virtualenv
- Create a new environment:
virtualenv myenv
(replacemyenv
with the name you want to give your environment) - Activate the environment:
source myenv/bin/activate
(on Linux/Mac) ormyenv\Scripts\activate
(on Windows) - Verify that you're using the correct environment:
which python
should point to the Python executable in your new environment
Method 3: Using python -m venv
- Create a new environment:
python -m venv myenv
(replacemyenv
with the name you want to give your environment) - Activate the environment:
source myenv/bin/activate
(on Linux/Mac) ormyenv\Scripts\activate
(on Windows) - Verify that you're using the correct environment:
which python
should point to the Python executable in your new environment
Common steps
- Once you've created and activated your environment, you can install packages using
pip install <package_name>
orconda install <package_name>
- To deactivate the environment, simply type
deactivate
(on Linux/Mac) ordeactivate
(on Windows) - To delete the environment, use
conda env remove myenv
(forconda
environments) orrm -rf myenv
(forvirtualenv
environments)
Remember to always activate your environment before working on a project, and deactivate it when you're finished. This will ensure that you're using the correct version of Python and packages for each project.