how to make python script executable when click on the file - python

I am trying to make my python script executable without going through the terminal typing like
python test.py
I want to make it able to run when i click on the file.
How i going to do this in my fedora machine.

Add #!/bin/python as the very first line of your file. Or, if you don't know where your python executable is, type which python in a terminal; then copy the result of that and put it after the #!.
Change the permissions of the file so that its executable chmod u+x test.py
i try but it still open back as gedit
Right click on the file in your gnome file browser or desktop.
Select Properties
Go to Open with and choose Python. If you don't see python in the list, add the command. Just type python in the command to be added.

Add #!/usr/bin/env python at the very beginning of file.
Make chmod u+x filename.py
Change your extension from .py to .sh, so your linux distro's UI will recognize it as shell script and try to execute.

It's Nautilus's fault. Open Nautilus (the file manager), go to Menu > Preferences.
Select the "Behaviour" section.
On the field titled "Executable text files", select the option "Execute executable text files when opened".

Add #!/usr/bin/python as the first line of the file and set the permission to executable chmod 755 yourfile.

If you don't have any specific version requirement then using first line as #!/usr/bin/env python will be more efficient and give the execute permission chmod u+x test.py

I know it can be too late, but i did have the same idea. (run python scripts in fedora) and found some trouble. My suggestion to you is to make a launcher with a .sh file, like this:
#!/bin/sh
gnome-terminal -x python yourscript.py
Give the permition to execute with chmod +x file.sh, them click and will run.
[^_~]

I use raspibian os (Linux)
Add #!/usr/bin/python as the first line of the file.py
right click file >> open with >> chose customize >> custom command line >> type python3
execute file with double click is working

Related

Is there a way to shorten command line commands to open Python scripts?

I'd like to know if there is a way to shorten what must be written on the cmd.exe command line to run Python programs. As it is, I have a program called Calculator.py and if I wanted to run that I would have to write:
python.exe C:\Users\user_name\restofdirectory\Calculator.py
Basically I want to know if I could make the 'Python' folder on my computer default so I could just type python.exe Calculator.py or some other similarly short way of doing it so I didn't have to type the whole directory.
Create a cmd.exe shortcut somewhere handy (Desktop or whatever). Right Click, select properties. Under the Shortcut tab you'll have a "Start In" field. It'll be default to wherever your cmd.exe normally opens. But you can change it to any path you want (So set it to your python development folder)-- then opening that shortcut will always start cmd.exe in the python folder.
You don't need to type python.exe file.py --
Just type: python file.py
So in short, you'd click that shortcut and type python file.py and you are done.
If you have specific scripts you want to run frequently, you can create a cmd.exe shortcut for each one. Set the Start In path to their folder. Then update the link to cmd.exe like this:
c:\path\to\cmd.exe /k python file.py
That will open a cmd prompt and automatically run that specific script
Yes, you can cd into the folder first.
If python is in your default path you can create a folder and put all your python scripts in it. Then cd into that folder and type python Calculator.py. To test if python is in your path simply open up a cmd and type cd \ (which in your case should change your working directory to C:\) then type python. If you get an error saying the command python cannot be found then you have to add python to your path. Other wise you should be able to run cd C:\Users\user_name\restofdirectory\ and then execute the script python Calculator.py
create an environment variable, say, PyPath = C:\Users\user_name\restofdirectory
then your line would be:
python.exe %PyPath%\Calculator.py
or you just cd to C:\Users\user_name\restofdirectory, and run
python.exe Calculator.py
Add Python to your path:
SET PATH=%PATH%;C:\Python27\bin
then you can call python like this:
python myapp.py
If you have the Python Launcher for Windows (comes with Python 3.3+) installed, then it gets even easier. You just need to put shebang in your code:
#! python
The first time you run your code, Windows will pop up a dialog and ask you what program to use to run the script. The Python Launcher should be listed. Just choose that and away you go. Here's a fun little script from the page I linked earlier:
#! python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))
Once my Windows 7 box had the launcher installed and I told it what program to run, I could just call my script from cmd.exe like this:
my_script.py
Note: If you have a version of Python older than 3.3, you can download the installer here

Run python script in terminal

I have created this python script called 'SleepCalc.py'. It is a simple script that tells me when to wake up based on 90 minute sleep cycles. Usually I open terminal, cd command my way to the dir containing the .py and then execute the script with the 'python' command.
What I want to know is how can I make an app/automator workflow/apple script that I can double click and it executes the python script in the terminal without me having to cd, etc.
http://codebin.org/view/98c0b7c5
Add shebang: #!/usr/bin/env python at the top of your script.
And then give the file permission to execute by:
chmod +x SleepCalc.py
You open Terminal and enter nano at the top. It opens up a Terminal-based text editor.
After that, type python followed by the file path (at the beginning, include ~/ because the computer starts at the root).
Do Control-X, click Yes, and save it as launch.command.
After that, type chmod +x and drag the newly created file into the Terminal window. This gives permission for the file to execute.
From then on, you can double click the .command file to launch SleepCalc.py.

How to run a python program in a shell without typing "python"

I am new to python. I wrote a program which can be executed by typing python Filecount.py ${argument} Why I see my teacher can run a program by only typing Filecount.py ${argument}. How to achieve that?
Make it executable
chmod +x Filecount.py
and add a hashbang to the top of Filecount.py which lets the os know that you want to use the python interpreter to execute the file.
#!/usr/bin/env python
Then run it like
./Filecount.py args
in linux-based OSs you must include a line (at the beginning of your script, i.e., the first line) like this:
#!/usr/bin/python
this tells the OS to seek your python interpreter at that location. this applies to any script.
remember to have the permissions in your script file (i.e. executable) for that to work.
Add a shebang line to the top of your file: http://en.wikipedia.org/wiki/Shebang_(Unix)#Purpose
It will tell the system which executable to use in running your program.
For example, add
#!/usr/bin/env python
as the first line, and then change the permissions of the file so you can execute it.
chmod +x Filecount.py
Best of luck!

starting Python IDLE from command line to edit scripts

I've tried many variations of this command: idle.py -e filepath, but it simply starts IDLE like normal, not opening any extra windows for editing, and not throwing any errors.
So how can I do the equivalent of opening IDLE, file>open>filepath via the command line (or perhaps even a Python module)?
You need to do as stated in the main.py file of the idelib folder (C:\Python33\Lib\idlelib), at least on the python 3.3 version explains that:
IDLE main entry point
Run IDLE as python -m idlelib
So with python -m idlelib <script_to_edit> you will be able to open and edit the script with idle. I haven't checked with previous versions but it could be the same comand
This is also documented on the changelog of the version 3.3.3
Make a new text file, and put something like this in it:
C:\Python26\Lib\idlelib\idle.pyw "C:\file1.py" "C:\file2.py"
In your actual script, you'll replace "C:\file1.py" and "C:\file2.py" with your files' paths, save as a .bat, and then launch it. That should do what you want.
Please forgive me for bumping such an old thread, but I've been teaching myself linux and python with the help of the community, and was trying to figure out how to invoke IDLE2 and IDLE3 from the command line. I came across this post some of the solutions seemed a bit complicated for my application. Then it occurred to me that I could just put syslinks in the /usr/bin/ path for each.
sudo ln -s idle-python3.1 idle3
sudo ln -s idle-python2.6 idle2
to address the OP. From the directory the script is located, type:
idle3 abc123.py
or
idle2 abc123.py
I'm just so damned happy that I finally had a "light bulb" go off that I wasn't going to let a 2 year old post stop me from posting my solution.
Rarely the native os is useful. I created a 'win batch file, in the folder with my .py files:
start /MIN cmd /C c:\Python27\lib\idlelib\idle.py -e %1 %2 %3 %4 %5 %6
This can open up to six files from cmd line in one shot. Just type the name of the batch file, followed by from zero to six filenames. Also if one or more files you specify are not found, idle opens these as new document(s).
first make sure you have location of idle in path
I am using "python3.5".So mine looks like this:
C:\Program Files\Python35\Lib\idlelib.Yours may differ.
use this following command:idle -r file_name.py to run the file
or just idle file_name.py to edit
or start idle -r file_name.py ^&exit
you can just program in Python to edit your Python files. A simple example. say you want to search for a word and then replace it with something else.
import fileinput
import os
os.chdir( os.path.join("c:\\","path") )
for file in os.listdir("."):
for line in fileinput.input(file,inplace=0):
if "search word" in line :
line=line.replace("search word","new word")
print line
(use inplace=1 to do in place editing.). Then save and run the script as normal Python script using the interpreter.
Just add IDLE's path to your PATH environment variable.
For example I created an environment variable called IDLE_PATH and set the value to C:\Python27\Lib\idlelib
Then in my PATH variable I added ;%IDLE_PATH%; and open a new cmd prompt or in console2 just open a new tab and run idle <file_name> to open the file, you will be able to do this from any directory. In IPython console add an ! before the command, for example !idle test.py.
Congrates, Now you're a python pimp!
paste the idlelib to your system path or user path, environment variable.for example, like this
C:\Program Files\Python310\Lib\idlelib
then type idle in your command prompt. done.

Auto executable python file without opening from terminal?

Sorry if this is on the wrong site ( maybe superuser ) but I'm trying to make my python.py file executable so I can click on it and it automatically does its thing, without me specifying it to open in the terminal by that default prompt, and I already have 'chmod +x' for its permissions.
Clarification:
I want to run it by clicking on it, not through the terminal ( I meant that when I said 'can click on it and it automatically does its thing ' )
Already have a shebang line
When I click it right now, it prompts me with do you want to open it in a text file, terminal - can I make it always default to opening in the terminal or is this just an oddball request?
On the first line in your python file, add this:
#!/usr/bin/env python
So if you have:
print "Hello World"
You should then have:
#!/usr/bin/env python
print "Hello World"
First, pick a file extension you want for files you want to have this behavior. pyw is probably a good choice.
Name your file that, and in your file browser associate that file type with python. In GNOME, you'd open its Properties window, go to the Open With tab, and enter python as a custom command.
Now here's the important part: That little dialog you've been getting asking you what you'd like to do with the file is because it is marked as executable. Remove the executable bit with chmod -x. Now when you double click it, it will simply be opened with the associated program.
Of course, if you want to run it from the command line, you'll now have to start it with python explicitly since it isn't marked executable. The shebang line doesn't matter anymore, but I'd leave it in anyway in case someone else marks it executable and expects it to work.
http://supervisord.org is better choice.
Have you placed this at the beginning of the file:
#!/usr/bin/python
?
As others have said, you need put the "shebang" at the start of the file, to say which interpreter to use to execute the file.
As mentioned in the above link, the most portable way is to use the env command (instead of a fixed path to python) - put this as the first line in the file:
#!/usr/bin/env python
The shell will look in $PATH for your python, rather than looking for /usr/local/bin/python then failing. This means it will work if Python is installed in a non-standard location.
For example:
$ cat example.py
print "Test"
$ file example.py # it is treated as an ASCII file
example.py: ASCII text
$ chmod +x example.py
$ ./example.py # when executed, it defaults to being executed as a shell script
./example.py: line 1: print: command not found
Now, if I add the "shebang" line...
$ cat example.py
#!/usr/bin/env python
print "Test"
$ file example.py # it is recognised as a Python script
example.py: a python script text executable
$ ./example.py # and executes correctly
Test
I have anaconda installed and
#!/usr/bin/env python
did not work for me, however:
#!/home/geoff/miniconda3/bin/python
did work. So, check which python your terminal normally uses to execute your .py files with
which python
in a terminal and use that as your shebang.

Categories