newville / pyshortcuts

create desktop shortcuts to python scripts on Windows, Mac, or Linux
MIT License
103 stars 15 forks source link

Some scripts require Conda activation #15

Closed maphew closed 5 years ago

maphew commented 5 years ago

On a Windows machine using Conda to manage it's python environments some scripts need more help than just creating a shortcut to python path/to/script.py. For example this will fail outside of conda activate xxxx:

# needs-activation-to-run.py
import sqlite3
print("Sqlite version:", sqlite3.sqlite_version)
$ c:\apps\Miniconda3\envs\leo-dev\python.exe needs-activation-to-run.py
Traceback (most recent call last):
  File "needs-activation-to-run.py", line 1, in <module>
    import sqlite3
  File "c:\apps\Miniconda3\envs\leo-dev\lib\sqlite3\__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "c:\apps\Miniconda3\envs\leo-dev\lib\sqlite3\dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ImportError: DLL load failed: The specified module could not be found.
$ c:\apps\miniconda3\scripts\activate.bat leo-dev

(leo-dev) Matt@SERVER D:\Matt\code
$ c:\apps\Miniconda3\envs\leo-dev\python.exe needs-activation-to-run.py
Sqlite version: 3.29.0

Discovered downstream at https://github.com/leo-editor/leo-editor/issues/1300

newville commented 5 years ago

@maphew, yes I noticed that too. I think that a wrapping batch script that activates the base environment is needed. I think I have a plan for that...

maphew commented 5 years ago

This seems to work here:

activate-and-call-test.bat: %windir%\system32\cmd.exe /K ""C:\apps\Miniconda3\Scripts\activate.bat" "C:\apps\Miniconda3" && %*"

In use:

Matt@SERVER D:\Matt\code
$ activate-and-call-test.bat python needs-activation-to-run.py

Matt@SERVER D:\Matt\code
$ C:\WINDOWS\system32\cmd.exe /K ""C:\apps\Miniconda3\Scripts\activate.bat" "C:\apps\Miniconda3" && python needs-activation-to-run.py"
Sqlite version: 3.29.0

I got the base syntax from https://github.com/conda/conda/blob/master/conda/shell/Scripts/activate.bat. The command is very sensitive to quote placement.

maphew commented 5 years ago

Possibly more complete wrapper:

%windir%\system32\cmd.exe /K ""C:\apps\Miniconda3\Scripts\activate.bat" && %* && conda deactivate"