0

I am using Ubuntu 12.04.

Hi I have been working on the following issue for a couple of days and as much as a I look at the many different answers (What is PATH environment variable and how to add it) I cannot make them work. I would be extremely grateful if anyone could detail me the required steps for a basic Ubuntu user:

I have Eclipse installed in my Dropbox folder and I have some Python .py files on it.

I have managed to add this folder to the path and now when I execute:

echo $PATH

I can see (among others) my folder with the python codes.

However when I try to run these files from any other folder:

$ python HelloWorld.py

I get:

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

UPDATE:

The solutions described below provide the main steps to make this work:

1) Add to your python scripts (.py) The "shebang" line

#!/usr/bin/python

This must be the FIRST line in your code.

2) Go to the folder where the python script is located and source it:

chmod +x yourscript.py

Once it is done you can run it from any folder (and the auto-complete will work). Incidentally, (this works in eclipse) if you duplicate an executable script within the editor, you can clear it and rename it and it shall remain executable. So you can avoid step two.

3

The command python is expecting a file in the current working directory with the name HelloWorld.py and that is why it's failing (it does not look in PATH for this, and this makes sense).


The PATH variable is for directories that contain executables (commands). If your Python files have the shebang:

#!/usr/bin/python

and are executable, you should be able to run them as:

$ HelloWorld.py

for example.


Say you want to add the directory ~/MyExecutables to PATH. You would add the line

PATH="~/MyExecutables:$PATH"

to your .bashrc file.

If ~/MyExecutables has an executable file called myexecutable you would be able to run it as

$ myexecutable <arguments>

To make a file myfile (in ~/MyExecutables) executable you'd run:

$ chmod +x ~/MyExecutables/myfile

Remarks. If a file is in a directory contained in PATH, but this file is not executable you won't be able to run it. Also if the file is a non-bash script and does not have the shebang line, it won't run even if it is executable.

  • Thank you very much!! That worked flawlessly! If I may two more questions. 1) Is there a way to avoid the chmod step? Making all my files executable directly? (Actually I have found that if you duplicate a code within eclipse it becomes executable itself) 2) In these forums how do I thank, give credit... – Delosari Jul 31 '13 at 22:03
  • 1) I'm not sure, maybe Eclipse has some setting related to this? 2) To say "thanks" just upvote the answers you think deserve some credit or had proven useful. – edwin Jul 31 '13 at 23:35
0

I believe you would have to add the folder to your python path for that approach to work. Instead, try adding a shebang to the top of your python file which should look something like this

"#!/usr/bin/python"

The path after the shebang "#!" Should be the path to your python interpreter which might be /bin/python. What this does is tell bash or whatever terminal your using to use python for the script.

After doing this you should be able to execute the file by just typing "HelloWorld.py" into the terminal.

Sorry for the quick answer. Ill give you further instruction if needed.

  • Thank you very much for your reply: The comment "to the top of your python file" was getting errors when I had it on the second line. How do I thank? – Delosari Jul 31 '13 at 22:05
  • If you feel the need you can click the up arrow at the top of my answer if you found it to be useful. However, I am just glad you found the answer to your problem. – Bryan Aug 1 '13 at 2:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.