Execute python script - python

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.

Related

Can't run Python script from Terminal without changing directories even though directory is included in PATH

OS: Mac 10.14.6
Python Version: 3.8.5
New to Python and Bash so apologies if this is a dumb question but I can't find an answer anywhere. The closest I found was this answer on this thread however, I've already executed chmod +x on that file to change the permissions to allow it to be executable and I followed the instructions again and I still couldn't get it to work.
Basically I want to run Python scripts from a specified folder on my desktop (file path ~/Desktop/Python\ Scripts) through Terminal without having to change directories (out of pure laziness).
I added the folder to PATH and can see that it is listed when I run echo $PATH in Terminal. I thought that would do the trick but when I try to run the program with the command python boxprintV2.py as I usually would when I change directories I get python: can't open file 'boxprintV2.py': [Errno 2] No such file or directory
This command works fine if I change the current directory as I have been doing and I can run my program no problem but I would like to run from a new terminal window without having to change directory every time. Permissions on the file have been changed using chmod +x
Shebang from my program is #!/usr/bin/env python3.
If you run the command python <filename>, the Python interpreter will only check the current directory. Therefore, this only works if your working directory is "~/Desktop/Python Scripts", as you have already found out.
Because your script is marked as executable and it includes a shebang at the beginning of the file, you can just execute it directly from the command line by only entering boxprintV2.py. Bash will then search all directories in $PATH for this file and execute it.
Ok, I've found a workaround by creating a shell script following this answer on a different thread.
What I did was open a blank textedit file, go to format and convert it to plain text (or ⇧ + ⌘ + T which toggles rich text/plain text).
From there I typed these commands into the document as follows:
#! /bin/bash
cd ~/Desktop/Python\ Scripts
python boxprintV2.py
When I saved I didn't specify a file extension and unticked the box that said "If no extension is provided, use .txt". I'm not sure if this was necessary but I'm just detailing my exact workflow for anyone else who may have the same (laziness) problem as I do.
I then went back into a blank terminal window and entered:
chmod +x ~/Desktop/Python\ Scripts/boxprintV2to allow the shell script to be executed by all users.
From here I can just open the Python Scripts folder on my desktop, double click on the plain text file which is now a .exe and a new terminal window is opened with my Python script running!
It's literally going to save me tens of seconds of my life. I'm sure I'll waste them anyway.

Execute Python scripts using file association without new window

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?

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.

Avoid typing "python" in a terminal to open a .py script?

Whilst running python scripts from my linux terminal, I find myself typing in python myfile.py way too much. Is there a way on linux (or windows) to execute a python script by just entering in the name of the script, as is possible with bash/sh? like ./script.py?
At the top of the script, put
#!/usr/bin/python
or whatever the path to Python is on your computer (the result of which python on Linux). This tells the system to run your script using python. You'll also need to do chmod +x script.py for it to work.
Or if you're really lazy, use alias p=python or something.
you will need to chmod 0755 script.py and as a first line in script have something like
#!/usr/bin/python
The first line of your Python script should be:
#!/usr/bin/env python
or
#!/usr/bin/env python3
Depending on your version, and if Python 3 is your default or not.
Then, set executable bits at the shell (maybe with sudo if needed):
chmod +x my_script_name.py
Note that with the above done, you could rename your Python script
mv my_script_name.py my_script_name
and then execute your Python script just by:
my_script_name
at the shell line.

Can't run Python script in cmd

I am just starting to write my first Python scripts and everything was fine, until yesterday when I tried to run a Python script in cmd. Usually, I type: cd Desktop, and than file.py. It always worked, but suddenly when I type file.py, it opens up the script in Notepad++ and it doesn't run the program in cmd. I hope that someone can help..
cd Desktop, and than file.py
Try
cd Desktop
then
python file.py
Notice the keyword 'python' before file.py
Guess in your windows env, .py file already being assign to open with notepad++
Right click any .py files, click properties, change the open with to your python executable, eg. C:\python27\python.exe
OR
Explicitly specify to run it with python in cmd
cd Desktop
python file.py

Categories

Resources