How apply command to file on $PATH with spaces?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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?







share|improve this question
























    up vote
    0
    down vote

    favorite












    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?







    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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?







      share|improve this question












      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?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 6 '17 at 19:29









      markling

      606




      606




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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.






          share|improve this answer



























            up vote
            2
            down vote













            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.






            share|improve this answer



























              up vote
              0
              down vote













              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.






              share|improve this answer




















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "106"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409284%2fhow-apply-command-to-file-on-path-with-spaces%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted










                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.






                share|improve this answer
























                  up vote
                  2
                  down vote



                  accepted










                  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.






                  share|improve this answer






















                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted






                    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.






                    share|improve this answer












                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 6 '17 at 19:54









                    Colin Watson

                    2,444128




                    2,444128






















                        up vote
                        2
                        down vote













                        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.






                        share|improve this answer
























                          up vote
                          2
                          down vote













                          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.






                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            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.






                            share|improve this answer












                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 6 '17 at 19:53









                            Weijun Zhou

                            1,434119




                            1,434119




















                                up vote
                                0
                                down vote













                                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.






                                share|improve this answer
























                                  up vote
                                  0
                                  down vote













                                  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.






                                  share|improve this answer






















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    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.






                                    share|improve this answer












                                    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.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 6 '17 at 19:54









                                    ivanivan

                                    3,1281213




                                    3,1281213



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409284%2fhow-apply-command-to-file-on-path-with-spaces%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        How to check contact read email or not when send email to Individual?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay