[PYTHON] [Windows + anaconda] Automatically activate the environment when the command prompt is started

background

I'm using python at windows + anaconda + command prompt. When working in a certain environment, it is very troublesome to open a command prompt and say ʻactivate hogehoge` etc. one by one. Therefore, I investigated how to automatically enter the environment when the command prompt is launched. ~~ You should use IDE ~~

environment

windows10 64bit anaconda 1.6.0

Conclusion

Let's write cmdrc

Reason

Official Environmental Operations Reference and [stackoverflow Questions](https://stackoverflow.com/questions/28436769/how-to-change- As far as default-anaconda-python-environment) is seen, it seems that (ana) conda does not support automatic activation of the environment. So, I thought that the easiest way is to create a file that is automatically executed when the command prompt is started and activate the environment in it.

Method

** Edit the registry. Please do the work at your own risk. ** **

Create a file called cmdrc.bat in an appropriate folder (here,C: \ Users \ USER \) and write the following contents.

cmdrc.bat


@echo off
cd /d %~dp0
if not exist "_TMP" (
type nul > _TMP
activate python_env
del "_TMP"
)

Replace python_env with your own environment. Once saved, open the Registry Editor and create a string value in HKEY_CURRENT_USER \ SOFTWARE \ Microsoft \ Command Processor with the name ʻAutoRun. The value data should be the path to cmdrc.bat`. image.png After saving this, open a command prompt and it should start with the environment activated.

Explanation of cmdrc.bat

@echo off: This command is set to execute the command without displaying it on the screen. Without it, all the commands executed in this file will be displayed on the screen.

cd / d% ~ dp0: This command moves the current directory to the location where the batch file is located. Immediately after starting the command prompt, the current directory is set in C: \ WINDOWS \ System32, so if it is left as it is, the _TMP file described later cannot be created.

if not exist "_TMP" (
type nul > _TMP
activate python_env
del "_TMP"
)

Instead of just ʻactivate python_env, we are changing the process depending on whether _TMP exists (if it does, do nothing). When I just typed ʻactivate python_env without writing a case, the computer consumed a lot of memory and the command prompt froze. Probably, when executing activate, a new batch file is started behind the scenes, batch file start → execute activate → activate starts batch file → that batch file executes activate… and the batch file is executed indefinitely I think it will end up. An infinite loop is prevented by creating a file called _TMP on the first run and not running ʻactivate` on subsequent batch files.

Reference site

http://c4se.hatenablog.com/entry/2012/10/19/230718 http://www.confrage.com/dos/batch-grammar/if/if.html http://d.hatena.ne.jp/nakamura001/20090203/1233652705 https://www.k-tanaka.net/cmd/del.php http://qiita.com/is_books/items/44d4cda4e11e194b8345 http://kiyotakakubo.hatenablog.com/entry/20090214/1234622897

Recommended Posts

[Windows + anaconda] Automatically activate the environment when the command prompt is started
[Anaconda] Activate the virtual environment
Make anaconda environment available from command prompt on windows
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
Browser does not open automatically when jupyter notebook is started
The story that the Homebrew environment was blown away when Anaconda was installed
Build Python environment on Ubuntu (when pip is not the default)
Error in ordinal number when importing Numpy in Anaconda environment of Windows
Goodbye to the command prompt in WSL, VSCode and Windows Terminal
A story when a directory is buggy with the django-admin start project command in a virtual environment using Pipenv
Anaconda python environment construction on Windows 10
Various Anaconda settings in Windows 10 environment
How to hide the command prompt when running python in visual studio 2015
Notify using Notification Center when the execution environment is macOS in Python
How to automatically notify by phone when the python system is down