Issue
I have got an applescript and a python script. The applescript should be the frame of the python script and should execute the python script. The python script uses a python version and packages, that are saved in a virtual environment.
How can I make the applescript run the python script inside of the virtual environment, so that all the packages and python versions of this environment are used?
My way to do this without applescript was to type source virtualenvironment/bin/activate
and after that python /Users/abc/script.py
into the terminal.
Using the applescript command
do shell script "source virtualenvironment/bin/activate"
do shell script "python /Users/abc/script.py"
does not work for me. Thanks in advance for your help !
Solution
You don't have to activate a virtualenv; that mostly just sets your PATH
environment variable so that when your shell looks up what executable to use for the python
command, it finds virtualenvironment/bin/python
before any other python executables. Just use the expanded, full path, so /virtualenvironment/bin/python
instead of python
:
do shell script "v/irtualenvironment/bin/python /Users/abc/script.py"
You can also make /Users/abc/script.py
executable by making the first line a shebang pointing to your virtualenv Python executable:
#!/virtualenvironment/bin/python
and setting the executable flag on the file (chmod +x script.py
, from a terminal).
Answered By - Martijn Pieters Answer Checked By - Gilberto Lyons (WPSolving Admin)