Calling a python file of virtual environment from applescript - python

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 !

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).

Related

How can I run a python script in anaconda environment with another python script? (not by .bat ro .sh file)

I have a python script which I want to run it in an anaconda environment because it uses some library like NumPy and I'm not going to install them.
I can easily write something like this:
# for Windows:
C:\ProgramData\Anaconda3\Scripts\activate.bat && python script.py arg
# for Linux:
source activate base
python script.py arg
but then I should have two scripts. a batch file and a shell script which is not desirable.
I like to have a single solution and independent of OS.
So I thought maybe it's good to have a python script which activates anaconda environment then run my script.(So I can handle OS there). I could do it by subprocess.call(). However, it gives an additional message which it's not desirable and I couldn't solve it even with a StackOverflow question.
Now I'm asking do you have a solution to run a python script in anaconda environment with another python script? (which of course doesn't give a notification like my solution.)

python virtual environment .exe file usage

What is the difference between running a script by first sourcing:
source /venv/bin/activate
python script.py
and running the script with the actual python exe?
/venv/bin/python script.py
do these two commands always do the same thing? The problem that I am seeing is that if script.py calls other python scripts, and the settings and packages that are in venv don't work.
It depends on how does script.py runs other scripts. If it uses sys.executable, that is the current python, /venv/bin/python then both commands are equivalent.
If script.py runs other scripts using shell (os.system, subprocess, etc) then the first one is the preferred form because it sets $PATH for all subprocesses so that all python scripts use the same virtual environment.
And the final note. If some script(s) being run from script.py have fixed shebang like #!/usr/bin/python those scripts will not be run in your virtual environment regardless of the 1st or 2nd way you run script.py.

Running commands within virtualenv in python

I am trying to run a python script under python 2 virtualenv. How to run it via batch script/ python script?
I have installed both python2 and python 3 and created virtual env too. I tried it invoking via python script but it didn't even enter the virtualenv. Then i tried the below batch script. But It just executed the first line of the code. i.e just activating the virtual environment. but other lines are not getting executed.
I even tried to execute the 1st line of batch script separately in a bat file and then invoke others using perl/python. but none of them worked.
Please do provide a way to execute these commands either using python script or
a batch file, which I will need to run it via perl/python
The batch file i used :
C:\venv-2\Scripts\activate
pushd <some path>
python test.py
deactivate
Just directly use the virtualenv's Python interpreter:
pushd some_path
c:\venv-2\scripts\python test.py
popd
Not having a Windows environment to hand, this is a non-answer for *nix which might be adapted to Windows.
Instead of calling the venv bin directly, try activating it within a script. For example:
# my-script.sh
# activate
source venv/bin/activate
# this should be in the venv
which python3
python3 -c 'print("Hello from python3")'
Then $ bash my-script.sh should print out which python3 it thinks is in use within the script.
See also A Python script that activates the virtualenv and then runs another Python script? for the same idea done better...
I got the the batch file executed by giving,
C:\venv-2\Scripts\activate & pushd <some path> & python test.py & deactivate
inside the batch file. And used perl script to call the batch file
system('start test.bat')

How can I activate a Python virtual environment and launch a python script, in a bash script on Mac?

I am trying to create a bash script that will simply
Activate my venv
Launch a python script
that can be double clicked and executed on Mac.
My script is as follows:
#!/usr/bin/env bash
./macVenv/bin/activate
python main.py
I can run from command line just fine. When I double click though it complains it cannot find the python file.
Yes. The following script works on my machine.
source activate $1
python $2
I run it by typing ./script_name py36 python_file_name
where script_name is the name of the script with these 2 lines. py36 is the name of the virtual environment and python_file_name is the python script you want to run.

Where is pyvenv script in Python 3 on Windows installed?

After reading the following statement from PEP 405
A pyvenv installed script is also provided to make this more
convenient:
pyvenv /path/to/new/virtual/environment
I tried to create a new virtual environment and failed miserably;
C:\>python --version
Python 3.3.1
C:\>pyvenv myvenv
'pyvenv' is not recognized as an internal or external command,
operable program or batch file.
Apparently pyvenv script is not installed into Scripts folder which is being usually added to the PATH environment variable making it possible to easily run such scripts on the command line.
Is PEP 405 wrong, was it not properly implemented in Python 3.3 or am I missing something?
It looks like pyvenv script is placed in Tools\Scripts subfolder inside Python installation folder (sys.prefix). It seems like copying it to Scripts subfolder is a good idea as it allows to simply type pyvenv from the command line (assuming Scripts folder is already on the PATH). As there's no exe wrapper for this script one has to make sure
.py extension is added to PATHEXT environment variable so that
Windows finds Python script placed on the PATH when typing script's
name at the command prompt.
.py extension is associated either with Python executable or with Python launcher (py.exe) which is available starting from Python 3.3
Alternatively one can just type python -m venv instead of pyvenv and save himself all of the hassle...
Related Python bug 17480 - pyvenv should be installed someplace more obvious on Windows
Use python -m venv someenvname instead.
Moreover, there is no strong reason to add python folder to PATH if you use system-wide python.exe only for creating virtual environments.

Categories