简体   繁体   中英

Running Anaconda environment from cmd Python

When I open the anaconda prompt the window that opens, shows:

(base) C:\Users\sherv>

So I deactivate it and get:

C:\Users\sherv>

which is the same thing when I open cmd. From here there is there any way that I can reactivate conda? Because the command words like "conda -v" or "activate base", etc, don't work.

Also, the conda prompt is a shortcut so when I right-click and select "open file location", it goes to windows\system32\cmd. So I don't understand why I can't activate conda environments from cmd if it's the same thing?

This is the anaconda prompt shortcut: Anaconda Prompt.lnk

When I open file location its the cmd: 打开文件位置后

I have even tried to add it to that path just in case it might work?

文件路径

PS. I'm very new to all this and trying to connect some dots; sorry if it's a stupid question.

ok, so the path first opens the cmd (which is why the shortcut points at it, but then from there, from within the cmd, it runs C:\Users\sherv\Anaconda3\Scripts\activate.bat. Is there anyway I can have the batch file commands run from cmd without having to write out the path every time? I added the path for the batch file but it didn't work

The reason why the anaconda prompt shortcut leads to cmd is because it needs to open cmd first and then run a batch file which activates the conda environment. What I had done was to add the batch file to the path but I wasn't using the correct commands to run the conda environment. Therefore, all the keywords such as activate etc didn't work.

Simply type activate.bat and the file which is added to the path is opened which takes you to the base conda environment.

You can use this method to run different programs on different APIs and export the result back and analyse it.

Thanks for downvoting my question:)

I figured out how to do it, in my case with miniconda so it might be slightly different.

  1. Find conda.exe in the Scripts folder. For me it was C:\Users\[SomeUser]\miniconda3\Scripts\conda.exe
  2. Open command prompt and navigate to that same directory
  3. Run conda init cmd.exe - To see more info on this command, you can run conda init --help , where it says "Initialize conda for shell interaction. [Experimental]"
  4. Now after reloading command prompt, from anywhere you should be able to do conda activate WhateverEnvName and it will start it in that environment

If you want to make a batch file that opens to the environment, you can use cmd.exe /k conda activate WhateverEnvName


Alternatively , if you don't want to just open the environment, but actually want to launch a python script directly using that environment, you can type the full path to the python.exe in that environment folder, and then the relative path of the python script to run with that environment.

For example, say you open the command prompt in some arbitrary directory that contains "myscript.py" that you want to run with an environment called "whateverEnv".

You'd do: C:\Users\SomeUserName\miniconda3\envs\whateverEnv\python.exe myscript.py

You could also put that same line into a batch file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM