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 ~~
windows10 64bit anaconda 1.6.0
Let's write cmdrc
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.
** 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`.
After saving this, open a command prompt and it should start with the environment activated.
@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.
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