You are not logged in.
Pages: 1
I've just started learning python and I have some questions. Where do I save .py files and how do I make them executable?
I can run this in the directory it is stored in:
$ python helloworld.py
Hello World
but when I run 'python helloworld.py' or just 'helloworld.py' when in a different directory it doesn't work. Although I did:
$ chmod +x helloworld.py
to make it executable. Can anyone advise?
Also what is the difference between '#!/usr/bin/env python' and '#!/usr/bin/python'?
Last edited by guriinii (2010-10-12 23:01:36)
The exponential learning curve.
Offline
To run executable files from any directory they have to be in your PATH:
echo $PATH
to see the current settings.
You can create e.g. ~/scripts and add that dir to the PATH. Now you put all your scripts there, make them executable and they should work.
Alternatively, you can create aliases / functions and put them in your ~/bashrc (or whatever you use):
alias whatdidido="sudo /home/karol/docs/code/whatdidido"
Last edited by karol (2010-10-12 23:27:18)
Offline
Your working directory is probably not in $PATH
Try launching with ./helloworld.py
You may also need to add the following as the first line in your file:
#!/usr/bin/python
Edit: Fixed #!, The path is not /bin/python
Last edited by ewaller (2010-10-12 23:27:11)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
@ ewaller
What about python2 / python3? If I want my script to use python2 how do I call it - #!/usr/bin/python2?
Edit: I am talking about http://mailman.archlinux.org/pipermail/ … 17653.html
Last edited by karol (2010-10-12 23:34:37)
Offline
@ ewaller
What about python2 / python3? If I want my script to use python2 how do I call it? IIRC it was 'python2'.
/usr/bin/python is probably a symbolic link:
ewaller@odin:~ 1020 %which python
/usr/bin/python
ewaller@odin:~ 1021 %ls -l $(which python)
lrwxrwxrwx 1 root root 9 Mar 31 2010 /usr/bin/python -> python2.6
ewaller@odin:~ 1022 %
In my case, it runs python 2.6
If you want to explicitly run a particular version, change the 'crunch bang' ( The #!) to point at the interpreter you desire:
#!/usr/bin/python2.6
or
#!/usr/bin/python3.0
etc...
Remember that the #! tells the system how to interpret the script. That way the shell knows whether it is python, perl, bash, ruby, octave, whatever....
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
I'm reading A Byte Of Python - Swaroop CH and the answer was on the next page. ooops. I'm far too eager or impatient.
The exponential learning curve.
Offline
Also what is the difference between '#!/usr/bin/env python' and '#!/usr/bin/python'?
distros may differ in where they install the python executables. python3 could be installed under /opt, for instance.
given this, it's more portable to use '#!/usr/bin/env python' (or '#!/usr/bin/env python3').
these forms will search for python in the $PATH rather than relying on a hard-coded location.
Offline
Thanks for this info, kachelaqa. I was wondering what that means, too, but haven't researched on that topic.
Offline
Pages: 1