Skip to content
Advertisement

Opening Anaconda Prompt with python

I want to create a script that will help me with installing packages for Spyder. Therefore, I want to use Python and have the script first open Anaconda Prompt, then execute the commands and then close that window.

However, I can get it to open for example the calculator but it doesn’t seem to open the command prompt. For opening the calculator I used:

import os
os.popen("C:\Windows\System32\calc.exe")

How can I get it to open the command prompt?

Advertisement

Answer

We have to combine a little knowledge of Windows batch language, some guidance from a previous SO answer, and extract key information from your specific Windows Start Menu shortcut which you would typically use to fire up the Anaconda Prompt.

First, from the Start Menu, right-click on the “Anaconda Prompt” shortcut and select “Open file location”. In File Explorer, right-click on the shortcut file and open its Properties. In the Shortcut tab, take a close look at the command in the “Target:” field. If you were to run this command in a Command Prompt window, it should turn that Command Prompt window into an Anaconda Prompt for all intents and purposes.

We just need the part of the command in the “Target:” field that is after the call to cmd.exe "/K". It should look something like this, calling an activate.bat file within the Anaconda install directory (or “miniconda“, the slimmer version which I use):

C:Users%USERNAME%miniconda3Scriptsactivate.bat C:Users%USERNAME%miniconda3 

The argument C:Users%USERNAME%miniconda3 determines the specific Conda environment that is loaded upon startup of the Anaconda Prompt. Keeping it as a path to the base miniconda3 folder will load the “base” environment, the default when you fire up your Anaconda Prompt from the Start Menu shortcut. If you would like to load a different environment, make this a path to C:Users%USERNAME%miniconda3envscustom-env-name instead (point being, append envscustom-env-name to your Anaconda/miniconda install dir).

For the purpose of this example, let’s refer to [this text of two paths that runs activate.bat to the designated Conda environment] as START_ANACONDA_CMD. As for the command(s) you want to run to install packages in the designated Conda environment, let’s call those PACKAGE_INSTALL_CMD_1 and PACKAGE_INSTALL_CMD_2.

The os.system call you should try executing from Python is as follows:

os.system(r"""start "My Spyder Package Installer" /wait cmd /c "START_ANACONDA_CMD&PACKAGE_INSTALL_CMD_1&PACKAGE_INSTALL_CMD_2" """)

Notes:

Carries out the command specified by string and then stops.

  • Run a series of commands with cmd /c by concatenating the commands with the & character. Do not put additional " quotes around the commands beyond what is shown in the example.

  • I wrap the entire os.system command string in r""" """ because otherwise you would have to escape " quotes and any backslashes in paths.

Once you have your START_ANACONDA_CMD, a nice little test is to open a Python interpreter and run the following:

os.system(r"""start /wait cmd /c "START_ANACONDA_CMD&conda info&timeout /t 10" """)

This should open a Command Prompt window to the designated Conda environment, run conda info, and then wait 10 seconds before exiting.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement