0

My $PATH has /a/path/with spaces/in/it

I can run executables that sit on said path by simply typing their names.

But when I attempt to apply commands on them, they fail with can't be found error.

eg. I have a python script on the path, called prog.py

If I just type prog.py, my shell attempts to run it. So the path is working. But:

$ python3 prog.py

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

$ file prog.py

prog.py: cannot open `prog.py' (No such file or directory)

If I cd to the actual location of prog.py, then attempt the same commands, they run as expected:

$ file ./prog.py

./prog.py: Python script, ASCII text executable

$ python3 ./prog.py

True

Why does this happen? And what can I do to execute these commands (particularly python3) from wherever I wish?

2

This has nothing to do with the directory name containing spaces. When you run python3 prog.py or file prog.py, you're specifying a relative path, and standard Unix semantics mean that it will be opened from the current working directory. There's no sensible way to change that.

However, if you make the script executable (chmod +x /path/to/prog.py) and ensure that it has a correct #! line at the top (probably #! /usr/bin/python3) then you will be able to execute it from anywhere by saying just prog.py (not python3 prog.py).

Incidentally, it's normally a good idea for programs on $PATH not to contain an extension that specifies their implementation, since that's really a layering violation; I'd recommend calling it just prog (replace as appropriate) rather than prog.py.

2

I believe the issue is not with the space. Have you tried a path without a space?

The point is, $PATH are the paths to look for executables, not files of random kind.

For example, if you have an executable that complains about a missing library, you have to use $LD_LIBRARY_PATH to tell it where the file is. Adding the location of the file to $PATH won't help. On the other hand if you come across a missing library in compile time, you need another variable (namely the $LIBRARY_PATH).

So the best solution would be to add proper shebangs to your python scripts and put them somewhere in the $PATH. If you want to be able to import them anywhere you need to work with $PYTHONPATH.

0

It is how you are referencing the prog.py file

python3 prog.py says "in my current working directory there is a file named prog.py"

Your second example - where you use ./prog.py - you are explicitly stating "in the current directory you'll find a program named prog.py"

Your $PATH is only searched for commands - not arugments to commands.

If you want to call the python binary (or any other binary like cat or sed or less or ...) then it should be in your $PATH - which you seem to have. To reference any other file you must always supply a full relative path or full absolute path to the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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