Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kerm1t/88916958b75b461404c02d45de4ff785 to your computer and use it in GitHub Desktop.
Save kerm1t/88916958b75b461404c02d45de4ff785 to your computer and use it in GitHub Desktop.

Revisions

  1. @maximlt maximlt revised this gist Jun 17, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions run_python_script_in_conda_env.bat
    Original file line number Diff line number Diff line change
    @@ -19,15 +19,15 @@ rem Using call is required here, see: https://stackoverflow.com/questions/246781
    call %CONDAPATH%\Scripts\activate.bat %ENVPATH%

    rem Run a python script in that environment
    python -c "import sys; print('Executed by: ', sys.executable)"
    python script.py

    rem Deactivate the environment
    call conda deactivate

    rem If conda is directly available from the command line then the following code works.
    rem call activate someenv
    rem python -c "import sys; print('Executed by: ', sys.executable)"
    rem python script.py
    rem conda deactivate

    rem One could also use the conda run command
    rem conda run -n someenv python -c "import sys; print('Executed by: ', sys.executable)"
    rem conda run -n someenv python script.py
  2. @maximlt maximlt revised this gist Jun 17, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion run_python_script_in_conda_env.bat
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ call conda deactivate

    rem If conda is directly available from the command line then the following code works.
    rem call activate someenv
    python -c "import sys; print('Executed by: ', sys.executable)"
    rem python -c "import sys; print('Executed by: ', sys.executable)"
    rem conda deactivate

    rem One could also use the conda run command
  3. @maximlt maximlt revised this gist Jun 17, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions run_python_script_in_conda_env.bat
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ rem - cmd.exe to be initialized with conda init
    rem Define here the path to your conda installation
    set CONDAPATH=C:\ProgramData\Miniconda3
    rem Define here the name of the environment
    set ENVNAME=mikesdk
    set ENVNAME=someenv

    rem The following command activates the base environment.
    rem call C:\ProgramData\Miniconda3\Scripts\activate.bat C:\ProgramData\Miniconda3
    @@ -25,9 +25,9 @@ rem Deactivate the environment
    call conda deactivate

    rem If conda is directly available from the command line then the following code works.
    rem call activate mikesdk
    rem python -c "import clr; print(clr.__version__)"
    rem call activate someenv
    python -c "import sys; print('Executed by: ', sys.executable)"
    rem conda deactivate

    rem One could also use the conda run command
    rem conda run -n mikesdk python -c "import sys; print('Executed by: ', sys.executable)"
    rem conda run -n someenv python -c "import sys; print('Executed by: ', sys.executable)"
  4. @maximlt maximlt created this gist Jun 17, 2020.
    33 changes: 33 additions & 0 deletions run_python_script_in_conda_env.bat
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    @echo OFF
    rem How to run a Python script in a given conda environment from a batch file.

    rem It doesn't require:
    rem - conda to be in the PATH
    rem - cmd.exe to be initialized with conda init

    rem Define here the path to your conda installation
    set CONDAPATH=C:\ProgramData\Miniconda3
    rem Define here the name of the environment
    set ENVNAME=mikesdk

    rem The following command activates the base environment.
    rem call C:\ProgramData\Miniconda3\Scripts\activate.bat C:\ProgramData\Miniconda3
    if %ENVNAME%==base (set ENVPATH=%CONDAPATH%) else (set ENVPATH=%CONDAPATH%\envs\%ENVNAME%)

    rem Activate the conda environment
    rem Using call is required here, see: https://stackoverflow.com/questions/24678144/conda-environments-and-bat-files
    call %CONDAPATH%\Scripts\activate.bat %ENVPATH%

    rem Run a python script in that environment
    python -c "import sys; print('Executed by: ', sys.executable)"

    rem Deactivate the environment
    call conda deactivate

    rem If conda is directly available from the command line then the following code works.
    rem call activate mikesdk
    rem python -c "import clr; print(clr.__version__)"
    rem conda deactivate

    rem One could also use the conda run command
    rem conda run -n mikesdk python -c "import sys; print('Executed by: ', sys.executable)"