Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1504  / 2 Years ago, mon, may 30, 2022, 10:02:34

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.


More From » bash

 Answers
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.


[#30140] Tuesday, May 31, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
measord

Total Points: 259
Total Questions: 131
Total Answers: 106

Location: Venezuela
Member since Sun, Oct 2, 2022
2 Years ago
;