How to Activate an Anaconda Environment

How to activate an Anaconda environment

If this happens you would need to set the PATH for your environment (so that it gets the right Python from the environment and Scripts\ on Windows).

Imagine you have created an environment called py33 by using:

conda create -n py33 python=3.3 anaconda

Here the folders are created by default in Anaconda\envs, so you need to set the PATH as:

set PATH=C:\Anaconda\envs\py33\Scripts;C:\Anaconda\envs\py33;%PATH%

Now it should work in the command window:

activate py33

The line above is the Windows equivalent to the code that normally appears in the tutorials for Mac and Linux:

$ source activate py33

More info:
https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/8T8i11gO39U

Does `anaconda` create a separate PYTHONPATH variable for each new environment?

How to activate an Anaconda environment from the command line?

To create an environment:
conda create --name myenv

NOTE: Replace myenv with the environment name.

When conda asks you to proceed, type y:

proceed ([y]/n)?for more detail click this

about activate this may help

to add anaconda to path

In Windows, you will have to set the path to the location where you installed Anaconda3 to.

For me, I installed anaconda3 into C:\Anaconda3. Therefore you need to add C:\Anaconda3 as well as C:\Anaconda3\Scripts\ to your path variable, e.g. set PATH=%PATH%;C:\Anaconda3;C:\Anaconda3\Scripts.

Best way to activate my conda environment for a python script on a windows PC

Not a Windows user, but conda activate is for interactive shell sessions. The conda run command is for programmatic execution within an environment. So, you would have a script with a line like:

conda run -n my_env python your_script.py

or possibly

conda run -p /path/to/my_env python your_script.py

if trying to share the environment across users.

If the script requires interaction, you may need to add flags (like --live-stream and/or --no-capture-output). See conda run --help.

Automatically activating conda environment in integrated terminal

Open Powershell and run as Administrator, execute the following command

conda config --set auto_activate_base true

Then restart VS Code, add the following in User Settings.json:

"python.defaultInterpreterPath": "\\path\\to\\conda\\python.exe",
"Python.terminal.activateEnvironment": true,
"Python.terminal.activateEnvInCurrentTerminal": true

Reload Window from Command Palette, select base:conda as python interpreter then press Ctrl+Shift+` to open a new integrated Terminal, conda environment should be activated automatically in it.

Activate conda environment without conda

Basic Pip exporting

Let's assume the environment is /path/to/env, with a /path/to/env/bin/python installed. If one only wants the Python packages, and pip is installed, then probably sufficient to do:

/path/to/env/bin/python -s -m pip list --format=freeze > requirements.txt

Note the -s flag insulates the site module from include packages in a user site (e.g., ~/.local/lib/pythonX.Y/site-packages/); if you want those included for some reason, then drop the flag.

Mimicking Conda activation

More diligently, one could simulate a basic activation by

  1. Prepending environment's bin/ to PATH; and
  2. Running any activation shell scripts (found in etc/conda/activate.d/); note that sometimes there are no such scripts and that folder doesn't exist.

Then exporting could be done with python -s -m pip list --format=freeze.

In most cases, I wouldn't expect a difference here, but I include it for completeness. We can't rule out that a package might be out there that manipulates environment variables via activation scripts in such a way that pip list output is changed. Not saying I've seen this, only that it's possible.



Related Topics



Leave a reply



Submit