executing python file

how do i execute python files as a application?
sorry if this is stupid question i am new to linux

ryuguns wrote:

>
> how do i execute python files as a application?
> sorry if this is stupid question i am new to linux
>
>
Add the following line on the top of your script so that it is the first
line

#! /usr/bin/env python

make your script executable with chmod

chmod +x mypythonscript.py

In addition to what Martin said you can also run the python files using the “python” interpreter from a command line. For example to run a file called “test.py” open up a console, navigate to the directory where the file is located and type

python test.py

example your py file name is “file.py”
you can type


python file.py

Hi,
There is no way I can make my python program work…

I put in the first line :

#! /usr/bin/env python

Then I try to make my script executable with chmod

chmod +x mypythonscript.py

Which gives me :

no such file or directory

So I make it executable manually, with a right clic (prop/access/‘is executable’).

Then I double click on my file : of course, it doesn’t start.

So I try in the console :

python file.py

And I get :

python: can't open file 'file.py': [Errno 2] No such file or directory

I must have missed something stupid and important…

no such file or directory

Looks like you are not in the directory of the file in the terminal. You can change to it (via “cd”) or give the full path to the file:

python /home/myuser/whatever/mypythonscript.py

On Mon, 17 Dec 2012 18:06:01 +0000, franck210883 wrote:

> I must have missed something stupid and important…

You’ve used two different names for the file - file.py and
mypythonscript.py - you need to use a consistent name. :slight_smile:

On the chmod, the error message is saying that that filename doesn’t
exist in the directory. Maybe try using tab completion.

Also, check and see if /usr/bin/env exists on your system - in my python
scripts, I use use the path to the python executable, and it works fine
for me.

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

Jim Henderson wrote:
> Also, check and see if /usr/bin/env exists on your system - in my python
> scripts, I use use the path to the python executable, and it works fine
> for me.

I’d be very worried if it doesn’t! :slight_smile:

On a standard system, using /usr/bin/env python or /usr/bin/python
result in the same effect. On a non-standard system, or one with
multiple python versions installed, they can have different effects.

/usr/bin/env helps with such systems, but it’s not infallible. People
can set up crazy systems where it doesn’t work.

FWIW, I always use the absolute path to the interpreter, too.

On 2012-12-17 19:06, franck210883 wrote:
>
> Hi,
> There is no way I can make my python program work…
>
> I put in the first line :
>
> Code:
> --------------------
> #! /usr/bin/env python
> --------------------

I’m looking at python files on my system and I see:


#!/usr/bin/python

Are you sure that your method is supposed to work?
And if it is, is the space at the start allowed?


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))