Skip to content Skip to sidebar Skip to footer

Condaenvironmenterror: Cannot Remove Current Environment. Deactivate and Run Conda Remove Again

Manage your Python Virtual Environs with Conda

Toggle between your Python 2 and Python 3 environments with ease

Photo by Denis Pavlovic on Unsplash

Although Python 2 is officially deprecated (Python 3, your fourth dimension is now!), I believe some of us even so have to maintain existing Python ii projects before fully port those projects to Python three.

In this article, I will show you lot how to manage your Python virtual environment by using Conda. Conda is a package and surround management system, it allows us to create and switch environments hands on our local motorcar.

Using Conda Surround

I will be using the iTerm last in macOS. Windows user volition have to use Anaconda Prompt if you never add the Conda environment path to your windows environment variable.

ane. Display Conda environment information

          % conda info        

To show information about current Conda install.

iTerm last on macOS showing conda info
Anaconda Prompt on Windows showing conda info

2. List all existing environments

          (base) username % conda env list          # conda environments:
#
base * /Users/username/opt/anaconda3
py2 /Users/username/opt/anaconda3/envs/py2

Nosotros can apply conda env listing to list all existing Python environments. The * will point to the current active environment. base is always the default agile environment when you open your final. py2 is my some other virtual surround for my Python 2 projects.

3. Create a new environment

          (base) username % conda create --proper noun project-env python=iii.seven        

Allow's say you desire to create a virtual environment for your new project, nosotros tin use conda create to create a new environment named project-env. We tin can also specify the Python version when creating the surroundings.

After creating your new environment, yous can run conda env list to check your new environment.

          (base) username % conda env list          # conda environments:
#
base * /Users/username/opt/anaconda3
project-env /Users/username/opt/anaconda3/envs/projection-env
py2 /Users/username/opt/anaconda3/envs/py2

The environments created past Conda is e'er located in /Users/.../anaconda3/envs/. You may change the default location by using the following command merely it is not encouraged. Conda can no longer detect your environment by your environment proper name, you will have to specify the environment's full path to activate it every time.

          (base) username % conda create --prefix /path/projection-env        

4. Activate your new environment

As y'all tin see, you have successfully create your new Python environment. Now we take to activate and apply information technology.

          (base) username % conda activate project-env          (project-env) username %        

The (project-env) indicates the current agile environment.

You can use conda list to display all packages in this surroundings.

          (project-env) username ~ % conda list          # packages in environment at /Users/username/opt/anaconda3/envs/project-env:
#
# Name Version Build Aqueduct
ca-certificates 2020.1.i 0
pip 20.0.2 py37_1
python 3.7.6 h359304d_2
readline 7.0 h1de35cc_5
setuptools 45.1.0 py37_0
...
...

conda list also supports revision history. Information technology is an incredibly useful feature to have to manage your package in the surroundings.

          (projection-env) username ~ % conda list --revision          2020-02-03 00:01:32  (rev 0)       # new surroundings
+ca-certificates-2020.one.1
+pip-twenty.0.ii
+python-3.7.six
+readline-7.0
+setuptools-45.1.0
...
2020-02-03 00:24:55 (rev ane) # installed numpy
+blas-1.0
+intel-openmp-2019.iv
+libgfortran-iii.0.1
+mkl-2019.iv
+mkl-service-2.3.0
+mkl_fft-i.0.xv
+mkl_random-ane.i.0
+numpy-1.xviii.1
+numpy-base-1.18.one
+six-one.14.0
2020-02-03 22:fourteen:24 (rev two) # installed configparser
+configparser-3.5.0

Imagine you lot accidentally screw up your dependencies just considering you update/install a new package. Revision history allows you to rollback your environment to the previous version only like that.

          (project-env) username ~ % conda install --revision i          ## Package Plan ##          environment location: /Users/username/opt/anaconda3/envs/project-env          added / updated specs:
- configparser
- numpy
- python=3.vii
The following packages will exist REMOVED: configparser-three.5.0-py37_0

configparser has been removed successfully. You can use conda list — revision to check again.

          (project-env) username ~ % conda list --revision          2020-02-03 00:01:32  (rev 0)       # new environment
+ca-certificates-2020.i.one
+pip-20.0.2
+python-three.seven.half-dozen
+readline-seven.0
+setuptools-45.1.0
...
2020-02-03 00:24:55 (rev i) # installed numpy
+blas-1.0
+intel-openmp-2019.4
+libgfortran-3.0.1
+mkl-2019.4
+mkl-service-two.three.0
+mkl_fft-1.0.15
+mkl_random-one.1.0
+numpy-i.18.1
+numpy-base-1.18.ane
+half dozen-1.14.0
2020-02-03 22:fourteen:24 (rev 2) # installed configparser
+configparser-3.5.0
2020-02-03 22:17:02 (rev iii) # reverted to rev 1
-configparser-3.5.0

4. Deactivate/Alter your active environment

Yous tin ever employ conda activate or conda deactivate to switch between your environments. You tin directly activate the environs you lot wish to utilise past using conda actuate.

          (project-env) username ~ % conda actuate base
(base) username ~ %

conda deactivate will deactivate your electric current active environs and change to the default environs which is the base of operations environment.

          (project-env) username ~ % conda conciliate
(base) username ~ %

5. Remove your surround

While you are washed with this surroundings and wish to remove it. You lot can use conda env remove to remove the environment.

          (base) username ~ % conda env listing          # conda environments:
#
base * /Users/username/opt/anaconda3
project-env /Users/username/opt/anaconda3/envs/projection-env
py2 /Users/username/opt/anaconda3/envs/py2

Same thing as create, yous accept to specify the name of the surroundings you lot wish to remove by using --proper noun.

          (base) username ~ % conda env remove --proper name projection-env          Remove all packages in environment /Users/junetao/opt/anaconda3/envs/project-env:        

The project-env has been successfully removed from your environments.

          (base) username ~ % conda env listing
# conda environments:
#
base * /Users/junetao/opt/anaconda3
py2 /Users/junetao/opt/anaconda3/envs/py2

six. Bonus! Modify your environment in VS Code.

If y'all are a Visual Studio Code fans like me, you lot tin can modify your Python environment that y'all created without whatever control line.

  • Click the Python 3.7.4 at the bottom left corner of VS Lawmaking.
  • Select the preferred environment.
  • Washed!
Changing Python surround in VS Code

Now y'all have learnt how to manage your Python surround with Conda. Do drop me a comment if I made any mistake or typo. Cheers!

If yous enjoyed reading this piece, you might also enjoy these:

You tin can find links to my other works on Medium and follow me hither . Thanks for reading!

winklerparepithe.blogspot.com

Source: https://towardsdatascience.com/manage-your-python-virtual-environment-with-conda-a0d2934d5195

Enregistrer un commentaire for "Condaenvironmenterror: Cannot Remove Current Environment. Deactivate and Run Conda Remove Again"