executing python code

D

Darren Dale

Some time ago I asked about executing a python program or script. For
windows, I was informed that the .py extension could be added to some list
of executable extensions, and then I could just type "mycode" instead of
"python mycode.py". Great advice, worked like a charm.

I recently jumped ship, and have been running Gentoo Linux for about two
months. Is it possible to get the same behavior on Linux? I thought it
would have something to do with adding the #!/usr/bin/env python line to my
code, but I'm not sure what this is supposed to do (it didnt work, that
much I know.)

Could I get some advice?

Thanks,
Darren
 
M

Michael Fuhr

Darren Dale said:
Some time ago I asked about executing a python program or script. For
windows, I was informed that the .py extension could be added to some list
of executable extensions, and then I could just type "mycode" instead of
"python mycode.py". Great advice, worked like a charm.

I recently jumped ship, and have been running Gentoo Linux for about two
months. Is it possible to get the same behavior on Linux? I thought it
would have something to do with adding the #!/usr/bin/env python line to my
code, but I'm not sure what this is supposed to do (it didnt work, that
much I know.)

How didn't it work? What did you do and what happened? What errors
were printed? Did you use chmod to make the script executable? Is
the script in a directory that's in your PATH? Is Python installed
on the system?
 
J

John Hunter

Darren> Some time ago I asked about executing a python program or
Darren> script. For windows, I was informed that the .py extension
Darren> could be added to some list of executable extensions, and
Darren> then I could just type "mycode" instead of "python
Darren> mycode.py". Great advice, worked like a charm.

Darren> I recently jumped ship, and have been running Gentoo Linux
Darren> for about two months. Is it possible to get the same
Darren> behavior on Linux? I thought it would have something to do
Darren> with adding the #!/usr/bin/env python line to my code, but
Darren> I'm not sure what this is supposed to do

'#!/usr/bin/env python' will try and run the script using the default
python in your environment. If you wanted to specify a specific
python, you could use, for example

#!/usr/bin/python2.1

Darren> it didnt work, that much I know.

You need to set the executable bit on the file. windows determines
whether a file is executable based on the extension, and linux based
on the file mode
> chmod +x myscript.py
> ./myscript.py

The ./ part says to use the file in the current working directory, and
may be required if the current directory is not in your PATH. You can
copy myscript anywhere in your PATH (eg /usr/local/bin), resource your
bashrc file (or open an new terminal) and the just do
> myscript.py

or with tab completion ....
> myscTAB # should complete myscript.py

Finally, you may want to add a pybin dir to your home dir where your
personal scripts reside. If you do that, just add that to your path,
eg in your .bashrc file.

JDH
 
I

Ian Bicking

Darren said:
Some time ago I asked about executing a python program or script. For
windows, I was informed that the .py extension could be added to some list
of executable extensions, and then I could just type "mycode" instead of
"python mycode.py". Great advice, worked like a charm.

I recently jumped ship, and have been running Gentoo Linux for about two
months. Is it possible to get the same behavior on Linux? I thought it
would have something to do with adding the #!/usr/bin/env python line to my
code, but I'm not sure what this is supposed to do (it didnt work, that
much I know.)

chmod +x mycode.py
../mycode.py

Note: . (the present directory) probably isn't in your $PATH, so you
have to use ./ to tell the shell to look in the present directory. Do
"echo $PATH" to see where you can put it where it's always accessible.
You also have to include the extension. If you want, you can rename it
to leave off the extension, and that will work fine. You can also make
a symlink, like (as root):

cd /usr/local/bin
ln -s /path/to/mycode.py mycode

~/bin is also often in the $PATH, instead of putting it in /usr/local/bin.
 
D

Darren Dale

Michael said:
How didn't it work? What did you do and what happened? What errors
were printed? Did you use chmod to make the script executable? Is
the script in a directory that's in your PATH? Is Python installed
on the system?

Yes, I did chmod, the script is in the current directory.

$ mycode.py
bash: mycode.py: command not found

I just tried:

$ ./mycode.py

and that will execute.
 
M

Michael Fuhr

Darren Dale said:
Yes, I did chmod, the script is in the current directory.

$ mycode.py
bash: mycode.py: command not found

I just tried:

$ ./mycode.py

and that will execute.

That means the directory isn't in your PATH environment variable,
neither as a full path (/path/to/the/directory) nor as ".", which
means the current directory. For security reasons it's usually
wise to leave "." out of your PATH, so if you don't want to type
../mycode.py each time then put your scripts in a directory that
PATH knows about. As others have mentioned, a common place for
a user's private scripts is in ~/bin, that is, the "bin" directory
under your home directory. If it doesn't exist then you can use
mkdir to create it, and if it's not in your PATH then you'll need
to add it.
 
M

Mike Meyer

That means the directory isn't in your PATH environment variable,
neither as a full path (/path/to/the/directory) nor as ".", which
means the current directory. For security reasons it's usually
wise to leave "." out of your PATH, so if you don't want to type

Actually, those reasons relate to having "." early in your path. If
it's the last thing on your path, you won't trigger any trojan horses.
./mycode.py each time then put your scripts in a directory that
PATH knows about. As others have mentioned, a common place for
a user's private scripts is in ~/bin, that is, the "bin" directory
under your home directory. If it doesn't exist then you can use
mkdir to create it, and if it's not in your PATH then you'll need
to add it.

I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
get to invoke it without having to specify the extension, and don't
have to reinstall the file if I change it.

<mike
 
M

Michael Fuhr

Mike Meyer said:
Actually, those reasons relate to having "." early in your path. If
it's the last thing on your path, you won't trigger any trojan horses.

Putting "." lessens the chances of tripping over a trojan but it
doesn't eliminate the possibility entirely. Think about typos or
commands that you thought were earlier in your path but aren't --
the latter might happen, for example, if you move to a different
OS and aren't aware that some commands are in a location that's not
in your default path.

Relying on "." being in your path might also lead you to write code
that breaks for somebody who doesn't have "." in their path. An
example would be a Makefile or build script that runs other commands
in the same directory and assumes those commands will be in the
user's path. It's sloppy to make that assumption, but people get
careless and sloppy things slip by.

But this is getting off topic for Python.
I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
get to invoke it without having to specify the extension, and don't
have to reinstall the file if I change it.

I sometimes do that as well.
 
T

Thorsten Kampe

* Darren Dale (2004-11-05 23:47 +0200)
Some time ago I asked about executing a python program or script. For
windows, I was informed that the .py extension could be added to some list
of executable extensions, and then I could just type "mycode" instead of
"python mycode.py". Great advice, worked like a charm.

"PATHEXT"
I recently jumped ship, and have been running Gentoo Linux for about two
months. Is it possible to get the same behavior on Linux?
No.

I thought it
would have something to do with adding the #!/usr/bin/env python line to my
code, but I'm not sure what this is supposed to do (it didnt work, that
much I know.)

Could I get some advice?

You're confusing a lot of things. Something like "PATHEXT" doesn't
exist on Linux. So you always have to type the full name of the script
(or use tab completion).

You can type "./myapp.py" if the script has the "she bang" first line
mentioned above and the file is executable.

You can type "myapp.py" if the script has the "she bang" first line
mentioned above and the file is executable and the current directory
is in your path (which it is /not/ the default under Linux - contrary
to Windows - and which is not advised).

You can type myapp.py - even if the script is not executable and not
in your path and doesn't have a "#!" line if you use an advanced shell
like zsh (Z Shell) and add a suffix alias ("alias -s py=python") to
your .zshrc

Just typing "myapp" won't work.

Thorsten
 
N

Ng Pheng Siong

According to Mike Meyer said:
I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
get to invoke it without having to specify the extension, and don't
have to reinstall the file if I change it.

I find that I hardlink them. I'm also more likely to edit ~/bin/mycode than
~/src/mycode.py.

Cheers.
 
M

Mike Meyer

I find that I hardlink them. I'm also more likely to edit ~/bin/mycode than
~/src/mycode.py.

I keep the src in CVS, so I want to edit it. Given that, hardlinks
have a tendency to be pointing at the wrong thing.

<mike
 
S

Steve Holden

Mike said:
Actually, those reasons relate to having "." early in your path. If
it's the last thing on your path, you won't trigger any trojan horses.
Sorry, but the advice still stands. As long as commands are typed
correctly everything is OK, but someone (like me) who makes a lot of
typing mistakes runs the risk of triggering a trojan whose name is a
misspelling of a command. So the best advice about putting "." on your
path is "don;t do that".
I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
get to invoke it without having to specify the extension, and don't
have to reinstall the file if I change it.

<mike

That works too. Of course under Linux/Unix there's no particular reason
to use the .py extension in the first place.

regards
Steve
 
M

Mike Meyer

Steve Holden said:
That works too. Of course under Linux/Unix there's no particular
reason to use the .py extension in the first place.

Some editors use the extension to determine the language mode to run
in. And distutils uses .py extensions.

<mike
 
S

Steve Holden

Mike said:
Some editors use the extension to determine the language mode to run
in. And distutils uses .py extensions.

<mike

There is that. I was thinking only of the execution scenario.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top