Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I run code from a different directory with a bash script
    primarykey
    data
    text
    <p>I have been putting my code on github, but I've run into an implementation snag. I run the same code on many computers (including a computer that I do not have root access on).</p> <p>One piece of code (a bash script) calls some python code like:</p> <pre><code>python somecode.py </code></pre> <p>The shell will run the correct version of python, but it won't find somecode.py.</p> <p>What I've tried: </p> <p>Fail #1: I tried to add both the directory which contains somecode.py and the full path to the file to the PATH; to no avail. [Errno 2] No such file or directory</p> <p>Fail #2: I can make it work for one computer ONLY if I add the full path to the correct version of python in the top line: </p> <pre><code>#!/usr/local/cool/python/version/location </code></pre> <p>However this breaks it running on any other computer. </p> <p>Fail #3: I can also make it work if I make the bash script say: </p> <pre><code>python /full/path/to/github/place/somecode.py </code></pre> <p>but again, this only works for ONE computer because the paths are different for different computers.</p> <p><strong>What I really want to do</strong>: I want to be able to use the same code (both bash script and somecode.py) on multiple computers. </p> <p>Any suggestions about how to do this properly is welcome. Thanks!</p> <h2>Solution</h2> <p>Added: </p> <pre><code>#!/usr/bin/env python </code></pre> <p>To the top of my somecode.py code; </p> <pre><code>mv somecode.py somecode chmod +x somecode </code></pre> <p>Make sure PATH has /full/path/to/directory/with/somecode.</p> <p>Bash script now says only: </p> <pre><code>somecode </code></pre> <p>and it works.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload