I have installed Py-Appscript on my machine and it can be used with the Python installation at /Library/Frameworks/Python.framework/Versions/Current/bin/python.
I am trying to use this installation of Py-Appscript with an Automator service. To do this, I use the Run Shell Script action and then set the Shell to usr/bin/python (which is my only choice for Python, unfortunately).
The usr/bin/python does not appear to have access to my third-party modules and crashes on the line:
from appscript import *
Is there a way for me to give usr/bin/python access to my third-party modules?
OR
Is there a way to tell Automator to use /Library/Frameworks/Python.framework/Versions/Current/bin/python instead?
I need Automator to run the Python directly from the Run Shell Script action. Any action that calls Python scripts that are external to Automator (via bin/bash, for example) does not perform quickly enough to be useful.
Okay, I was able to get it working using a hack found at How do I execute a PHP shell script as an Automator action on Mac OS X.
Inside of the Run Shell Script action, I used the /bin/sh/ shell with <<EOF ... EOF to the proper Python installation.
So for example, entering
/Library/Frameworks/Python.framework/Versions/Current/bin/python <<EOF
from appscript import *
Numbers = app('Numbers')
EOF
Into the code section of the Run Shell Script action will work. So one can call the proper installation (/Library/Frameworks/Python.framework/Versions/Current/bin/python above) and put their program between the <<EOF ... EOF delimeters.
Alfred
This trick works with Alfred also. If you want to use appscript with Alfred, just make sure that you pass {query} to the python version above, like this:
/Library/Frameworks/Python.framework/Versions/Current/bin/python script.py {query}
When you install modules, you typically install them per Python instance. So in this case you have installed them for the Python in /Library/Frameworks/Python.framework/Versions/Current/bin/python, and it will then be available only for that Python. /usr/bin/python is then apparently another Python installation (I'm not an OS X expert).
To make it available for the /usr/bin/python installation, install it for /usr/bin/python.
Related
In my Python scripts I use the shebang syntax at the beginning of the script:
#!/usr/bin/env python3
I noticed that this mostly stopped working recently (on Windows with Python 3.11 and I believe also with 3.10.8) - like there were some changes to py.exe. When I run a script nothing happens:
# Nothing happens
myscript
# Nothing happens
myscript.py
# Nothing happens
py myscript.py
What's interesting is if I pass any options to py, then it works:
# Script invoked:
py -3 myscript.py
Also, if I change the shebang line to remove the 3 it works:
# Change beginning of script to this:
#!/usr/bin/env python
# Now script runs
myscript
# Now script runs
myscript.py
# Now script runs
py myscript.py
If py.exe consistently didn't work with the /usr/bin/env python3 shebang I would think perhaps it was a change. But since it works with any arguments, this makes me wonder if it's a bug?
Thoughts?
Updates:
Note: The shebang syntax is officially supported for Python for Windows, please see: https://docs.python.org/3/using/windows.html#shebang-lines
Further testing this out, I created the following test script - "myscript1.py":
#!/usr/bin/env python3
import sys
print(f'Testing running using Python {sys.version}')
Results using Python 3.10 for Windows (64-bit version):
myscript1
Testing running using Python 3.10.8...
Results using Python 3.11 for Windows (64-bit version):
myscript1
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
Results using Python 3.11 using python binary:
python myscript1.py
Testing running using Python 3.11.0...
Results using Python 3.11, call py launcher directly:
py myscript1.py
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
Results using Python 3.10's py launcher directly:
py310 myscript1.py
Testing running using Python 3.11.0...
Results using Python 3.11's py launcher directly with any argument:
py -b myscript1.py
Testing running using Python 3.11.0...
If I edit myscript1.py and change the shebang line to this:
#!/usr/bin/env python
Then it works:
myscript1
Testing running using Python 3.11.0...
So, it appears the py.exe launcher included with Python 3.11 does not work if the shebang line is like this:
#!/usr/bin/env python3
Unless an argument is passed - then it works.
This seems like a bug...
As per my knowledge, Windows have no proper shebang line. Shebang line normally used to say where executable is present/installed.
As per my observation
python3 - Linux
python - Windows or (python installed in linux <2.7)
This may be because Linux thought to keep different acronyms for differnt versions of python (Above 3+ -->python3 & Below that use python
On Windows, where many of these facilities don't exist, a common arrangement is for Python to be installed in C:\python3.1xx.xx\bin\Python.exe
More observations
windows
where python
##Gives
C:\Users\***\AppData\Local\Programs\Python\Python39\python.exe
Linux
type -a python3
##Gives
usr/bin/env python3
So logically
for Linux - #!/usr/bin/env python3
for windows - #!/usr/bin/env python or #!python (I always use this)
If you want to make shebang work on windows simply install pywin32. Logically it wont make work...But pywin32 install some additional set of modules that helps to run python related exicutables smooth as which it acts as some API between python & windows
I have a python script which I am distributing in several nodes. I have python 2.6 installed in /usr/bin by default and have python 2.7 in my /opt directory in all the nodes. Now when I run the script from my current node I can set the path to python 2.7 interpreter from terminal but I am unable to manage it in the rest of the nodes where this script is getting distributed. I have added the shebang at the start of script like -
#!/opt/python2.7/bin/python
But its still not working. How can I change the python interpreter/python path at the beginning of the script itself.
What you explain should work but check:
that the script is executable (chmod +x my_script.py if required).
that you are calling the script directly and not using another Python interpreter (check that you execute ./my_script.py or /path/my_script.py and not python my_script.py).
To help to diagnose the problem you could add the following lines to the top of your script:
#!/opt/python2.7/bin/python
import sys
print(sys.executable)
if the output is not /opt/python2.7/bin/python you might be calling the script with another interpreter.
If for some reason you can only call scripts executed by the 2.6 version of Python remotely but you can also distribute additional files, you could try to send your main script somewhere and execute the following auxiliary script:
from subprocess import call
call("/opt/python2.7/bin/python /path/my_scipt.py", shell=True)
I have 2 versions of python installed on windows, 2.7.3 and 3.3. Some of my scripts are 2.x and some 3.x. Is there an easy way when executing these scripts from a command line to direct them to the appropriate interpreter?
Note: For Windows use the new Windows Python launcher (available with Python 3.3 and downloadable here for earlier releases) which recognizes Unix shell shebangs. You can read about it here.
Most Linux distributions will create python2 and python3 aliases for the installed Python 2.x and Python 3.x interpreter (if not you can just create the symbolic links yourself anywhere on your $PATH, the env command will take care of finding them), so you should just need to set the appropriate interpreter as the first line of your script:
#!/usr/bin/env python2
or
#!/usr/bin/env python3
This will direct the shell to use the appropriate interpreter, if you set the script files to be executable and just invoke them directly on the shell. E.g.:
$ chmod +x script.py
$ ./script.py
Try this first: I am on OS X, but when I want to use Python 2.6 instead of Python 2.7 (its a numpy/scipy thing), I simply run python2.6 whatever.py to run whatever.py in Python 2.6. Try that first.
If that doesn't work, then you can use virtualenv - the virtual environment builder for Python.
http://pypi.python.org/pypi/virtualenv
I am sure there are similar alternatives, too.
Pedro Romano's answer is the most elegant way of doing this.
But if you don't want to download and install the Python launcher, create a batch file as described below. You can also create a shortcut, copy C:\Python27\python.exe to C:\Python27\python27.exe, etc.
I'm guessing C:\Python27 and C:\Python33 are already on your system path. If so, you can create a batch file called python2.7.bat in C:\Python27\ which contains:
C:\Python27\python.exe %1
and a similar file (e.g. python3.3.bat) in C:\Python33\
Now, you can run python2.7 script.py from anywhere in command prompt and it should work :)
Hello gyus i have started python and i want to know how can i excecute python file without using terminal.Just like the most games using (exe) file extension but for py files.I have tried py2exe but it doesn't show anything on the screen. I tried to make excecutable the py file with no luck. Please tell me how to excecute the and if there is an option whithout using a specific program for that.
My system is : Windows 7 / Ubuntu 12.04
Here, have a look in my blog, It explains how ca you do it in Ubuntu.
http://insidepython.wordpress.com/2012/08/04/hello-world-or-how-or-say-ni/
but basically:
Add this line to the beginning of your script
#!<location of your python interpreter>
To find out where your python interpreter is installed:
$ sudo find / -name "python"
After executing the previous, you should get the location of your python interpreter, then you need to set the environment variable, in my case python executable is located in /usr/bin/python
$ export PATH="$PATH:/usr/bin/python"
Then you need to set the file attributes to executable, you can look more into file attributes in Unix/Linux here http://en.wikipedia.org/wiki/Chmod
$ chmod +x shrubbery.py
And finally to execute your application
$ ./shrubbery.py
For Windows, there's also a python script called GUI2Exe that I have used in the past to create distributable versions of python scripts as apps. It's available at
http://code.google.com/p/gui2exe/ and is, in my opinion, very simple to use. Tutorials and whatnot are easy to find on Google.
It uses py2exe (or any other Python compiler library) to put together the script, but it doesn't require any syntactically annoying setup.py file.
I have installed the new python release and would like to run .py files from the terminal.
How is this done from the terminal? I dont want to include the path in each command to run a .py file.
If you want to override the python command, you can set your PATH variable correctly, e.g. in your ~/.bash_profile:
export PATH=/path/to/python/:$PATH
That said, for managing different versions of components that are also provided by Mac OS X, I suggest to use a package manager such as Homebrew.
if you add a shebang at the start of the python file then you can run a python file by just its name from terminal
add #!/usr/bin/python
for mac(others add your respective path for python)
at the top of your python program and from your terminal you can run it just by filename(if it has executable permissions).
Have a look at the Python package under Applications. There is a shell script there called Update Shell Profile.command
Run this and it should set your path up properly.
Unless you mark you script as executable with chmod +x, you'll need to run python over it first. e.g. `python myscript.py'
I installed all of my python through macports, which has pros and cons. One of the benefits is that you don't have to worry about stuff like this, it just works. You can install python 2.6 and python 2.7 (and others), and then use the python_select utility to set up which python is run when you call "python blah.py"
Since you have installed a working python, the easiest way to run python files from the terminal is to cd your terminal to the directory where the file is located and then just type python my_code.py in the terminal.