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 13 2091
Darren Dale <dd**@cornell.edu> writes: 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?
--
Michael Fuhr http://www.fuhr.org/~mfuhr/
Michael Fuhr wrote: Darren Dale <dd**@cornell.edu> writes:
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?
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.
Darren Dale wrote: 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.
--
Ian Bicking / ia**@colorstudy.com / http://blog.ianbicking.org
>>>>> "Darren" == Darren Dale <dd**@cornell.edu> writes:
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
Darren Dale <dd**@cornell.edu> writes: 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.
--
Michael Fuhr http://www.fuhr.org/~mfuhr/ mf***@fuhr.org (Michael Fuhr) writes: Darren Dale <dd**@cornell.edu> writes: 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
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Mike Meyer <mw*@mired.org> writes: mf***@fuhr.org (Michael Fuhr) writes: For security reasons it's usually wise to leave "." out of your PATH 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.
--
Michael Fuhr http://www.fuhr.org/~mfuhr/
* 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
According to Mike Meyer <mw*@mired.org>: 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.
--
Ng Pheng Siong <ng**@netmemetic.com> http://sandbox.rulemaker.net/ngps -+- M2Crypto, ZServerSSL for Zope, Blog http://www.sqlcrypt.com -+- Transparent AES Encryption For SQLite ng**@netmemetic.com (Ng Pheng Siong) writes: According to Mike Meyer <mw*@mired.org>: 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.
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Mike Meyer wrote: mf***@fuhr.org (Michael Fuhr) writes:
Darren Dale <dd**@cornell.edu> writes:
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
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". ./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
That works too. Of course under Linux/Unix there's no particular reason
to use the .py extension in the first place.
regards
Steve
-- http://www.holdenweb.com http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Steve Holden <st***@holdenweb.com> writes: Mike Meyer wrote: 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.
Some editors use the extension to determine the language mode to run
in. And distutils uses .py extensions.
<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Mike Meyer wrote: Steve Holden <st***@holdenweb.com> writes:
Mike Meyer wrote:
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.
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
-- http://www.holdenweb.com http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by David Faden |
last post: by
|
reply
views
Thread by Nick Coghlan |
last post: by
|
15 posts
views
Thread by Nick Coghlan |
last post: by
|
1 post
views
Thread by Valentina Boycheva |
last post: by
|
3 posts
views
Thread by Pankaj |
last post: by
|
2 posts
views
Thread by krishna.000.k |
last post: by
|
reply
views
Thread by Gordon Fraser |
last post: by
|
reply
views
Thread by Orestis Markou |
last post: by
|
3 posts
views
Thread by jim3371 |
last post: by
| | | | | | | | | | |