Search code examples
pythonwindowscmdminiconda

Run a python file that utilises conda packages with just a double-click on a file. (windows)


I want to run a python file to a windows computer that uses packages installed with miniconda with one double-click (this is very inmportant).

The computer that I will run it, does not have anything installed (you can think of it as a formatted pc).

I figured out to do it with .bat file and it works, but I have to run the file twice, since the first time after installing miniconda, the shell needs to restart. I searched but I did not find a command to restart the shell and continue with the execution of the python file in order to be done with just one double-click. So, only for the first time, it has to be done manually.

Is there a way to do it with a batch file or should I do it otherwise?

Here is my batch file

START %CD%\Miniconda3-latest-Windows-x86_64.exe /InstallationType=JustMe /AddToPath=1 /RegisterPython=0 /S /D=%CD%\miniconda3
CALL conda activate .\envs_dir
python python_script.py
PAUSE

Solution

  • The problem was that the installation changes the user environment variables and not the local environment variables. @Mofi explains it in detain on the comments of my post. I switched my batch file as follows:

    START %CD%\Miniconda3-latest-Windows-x86_64.exe /InstallationType=JustMe /AddToPath=1 /RegisterPython=0 /S /D=%CD%\miniconda3
    TIMEOUT 40
    set PATH=%PATH%;%CD%\miniconda3\Library\mingw-w64\bin;%CD%\miniconda3\Library\usr\bin;%CD%\miniconda3\Library\bin;%CD%\miniconda3\Scripts
    CALL conda activate .\envs_dir
    python python_script.py
    

    UPDATE

    For a more robust batch script, following the comments of @Mofi, I changed my script to:

    "%~dp0Miniconda3-latest-Windows-x86_64.exe" /InstallationType=JustMe /AddToPath=1 /RegisterPython=0 /S /D="%~dp0miniconda3"
    set "PATH=%PATH%;%~dp0miniconda3\Library\mingw-w64\bin;%~dp0miniconda3\Library\usr\bin;%~dp0miniconda3\Library\bin;%~dp0miniconda3\Scripts"
    call "%~dp0miniconda3\Library\bin\conda.bat" activate "%~dp0envs_dir"
    "%~dp0envs_dir\python.exe" "%~dp0python_script.py"