Execute Python scripts using file association without new window - python

Recent versions of Python include a py.exe launcher that will interpret Unixy shebang lines like #!/usr/bin/env python3 and run the script appropriately. If you associate .py files with py.exe you can even run them directly from a shell (e.g. Powershell) like this:
./myscript.py
Unfortunately it still seems to run the script in a new window. Is there any way to make it output to the current shell without having to run py myscript.py?

Related

specify the python interpreter path in python script

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)

Python 3.6.0 can't be installed on Ubuntu 16.04?

I wrote a script on my Windows machine using python 3.6.0. I wanted to run it on Linux machines as well so I transferred the .py script onto my Ubuntu VM. The problem is, I tried to install python 3.6.0 in every way possible but it doesn't really work. Python3 works when I want to execute a .py file, but since I am trying to use pyisntaller to convert it into an executable for linux, I need the default version as 3.6.0. python --version gives me Python 2.7.2. How can I fix this so when I do python --version it shows 3.6.0?
There is no point in changing your default python version, Ubuntu relies heavily on older versions of python. And what do you mean by convert it into an executable for Linux? Almost every file on your file system is an executable. If you want to make your program run as a command in the terminal you can do this though:
text_editor_of_your_choice .bashrc
This should open the hidden .bashrc file, and scroll to the bottom of it. Below is what the file looks like...
.bashrc
After that you can create a function for the python command you want to run.
eg. function ghst {python3.6 /home/user/example_script/example.py $#}
Start a terminal session and then try running it. Now that's if you want to run it inside the terminal. If it you want to create this "executable file" you speak of you can create a simple shell script.
First go in the terminal and run touch file_name_of_your_choice.sh
This will create a blank file named file_name_of_your_choice with the file extension "sh". After that open the text file in whatever directory you created it in, if you are unsure do pwd.
In your first line make sure that the user is inside the directory of the .py file by doing
cd /home/user/folder/ On the next line you should then execute the .py file. You can do this by typing this: python3 file.py. This will execute the program inside a terminal window. After you have done all of this make sure that the python file and the script file are executable. Do this by doing chmod +x file.py and chmod file.sh.
This is what the end result looks like
example script
You mentioned inside your question that you want your program to be able to run on Linux machines as well. Whether this is for personal or a public project of some sort it comes in handy by packaging all the required files in a .zip format this makes it easy to unpack everything when moving to separate machines. If there are any problems let me know.

Execute python script

I have been working with Python for a short while and have created some programs, Im just setting up a third Raspberry Pi with Raspian Jessie. On the other two Pis I didn't run into an issue, but on this one I can't get my Python script to execute the same way.
The first line of the file has the shebang:
#!/usr/bin/env python
I git cloned the repo and went to the directory and did:
chmod +x script.py
but when I try to ./script.py it doesnt work. From the Desktop it asks if the file should be executed and when I choose execute in terminal it closes and does not execute. I tried placing script.py in
/usr/local/bin
but that has no effect. If I do:
python script.py
it works fine.
The only difference between the Pis is the first two are headless and this new one has the Pixel desktop environment.
You probably made the file on a Windows PC and it got saved with DOS-style line endings. The linux shell (bash) doesn't like that and gets confused when trying to execute the script file.
Try to convert the file to UNIX line endings:
dos2unix script.py
You may need to do apt-get install dos2unix.
As a tip, you can use an editor/IDE in Windows that supports/saves as UNIX-style line endings to avoid this.

Can a Script be an Environment Variable

I have a Python script that I would like to be able to execute from no matter where. Either in Linux or in Windows, but in this case preferably in Windows. Putting the path to the script into PATH under Windows did not work, so from some directory calling python my_script.py results in the message that there is no such file in this directory. So, is this somehow possible?
You can try creating an alias as such:
Linux
Create a .bash_aliases file on your home folder
Add an alias such as alias pscript='python /home/pythonscript.py'
Log out and back in or do a source .bash_aliases
Windows
Run doskey pscript=python C:\script.py. Read more here
The PATH is used to search for executables, and this doesn't include in windows script files.
A workaround is to convert the script to a batch file, see here
how to simply have the script act also as a batch file
For the operating system to run your script, it needs to find it (the PATH variable), recognize that it is executable and know which program should execute it. You seem to have the PATH part handled, so now for the other two.
On unixy systems you need to make the script executable. To set the user-executable bit, do chmod u+x myscript.py. Then you need to tell the system which program should run it. Typically you use the "shebang" as the very first line in the file:
#!/usr/bin/env python3
The system will search the path for a program called "python3" (use "python" for python 2 scripts) and use that executable to run the script.
On Windows, you need to associate the file extension (.py) with the python exeuctable. That's usually done for you when python is installed. If not, you can dig into ftype, assoc and pathext here.
Windows doesn't care about the shebang (unless you are running cygwin, then see unixy systems above) so the same script can live in both worlds.
Once the script is executable, you call it directly instead of executing python and giving the file as the script name. Its just
myscript.py

Execute Python script from AutoIt

I have a Python (.py) file and need to execute it from AutoIt. How can I do this?
Python scripts can be executed from command line. For a script called myscript.py, you can run it (assuming Python is installed) by typing:
python myscript.py
If you want to run myscript.py without having to prefix it by python, then set the path to the Python binary (e.g. C:\Python27\Python.exe) as Windows environment variable. This enables AutoIt to execute the Python script as if it were an external program. Reference: ShellExecute().
ShellExecute("myscript.py")
Point to where myscript.py resides of course. Use:
RunWait("C:\Python27\Python.exe myscript.py")
to avoid setting environment variables.
RunWait("C:\Python27\Python.exe filename.py")
this code working fine run from autoit

Categories

Resources