1

I have a python program which is an interpreter, for a language that I have made. It is called cbc.py, and it is in a certain directory. Now, I want to know how I can call it, along with sys.argv arguments (like python3 cbc.py _FILENAME_TO_RUN_) in any directory. I have done research on the .bashrc file and on the PATH variable, but I can't find anything that really helps me with my problem. Could someone please show me how to resolve my problem?

martineau
  • 119,623
  • 25
  • 170
  • 301
Caspian Ahlberg
  • 934
  • 10
  • 19

2 Answers2

0

You need to make your script executable first and then add it to your PATH. If you have your python script at ~/path/to/your/script/YOUR_SCRIPT_NAME:

  • add #!/usr/bin/python3 at the top of you script,
  • give executable permision to your script using sudo chmod a+x YOUR_SCRIPT_NAME,
  • edit ~/.bashrc to add your script path, e.g. echo PATH="$HOME/path/to/your/script:$PATH" >> ~/.bashrc,
  • restart or re-login or run source ~/.bashrc,
  • now you can access your script via YOUR_SCRIPT_NAME anywhere.
webb
  • 4,180
  • 1
  • 17
  • 26
DumTux
  • 668
  • 1
  • 9
  • 24
  • This isn't working for me. I have added this to the top of my script: https://ibb.co/zZGx9DG I did the sudo chmod command in my terminal, and I added this to my script path: PATH=/Users/caspianahlberg/Users/caspianahlberg/Desktop/Programming/Python\ Files/cowboy/Simple_Shell_Test:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin (by the way, cbc stands for cowboy compiler). But, I just get this error: -bash: cbc.py: command not found - Hm, why isn't it working? – Caspian Ahlberg Dec 24 '19 at 03:52
  • Here is a sample line to add `/path/to/a/script/myscript.py` to PATH: `echo 'PATH="/path/to/a/script:$PATH"'` – DumTux Dec 24 '19 at 11:27
  • As I have never used Mac, I am sorry to not answering you correctly. – DumTux Dec 24 '19 at 11:38
  • That's not how the `PATH` var works. It contains a colon separated list of directories to search for commands. Putting the full path of the command in `PATH` is a no-op; i.e., it doesn't do anything. Also, the python3 binary may, or may not, be */usr/bin/python3*. It could just as easily be */usr/local/bin/python3* or some other directory. – Kurtis Rader Dec 24 '19 at 19:16
  • Then how does it work? What's the point in using the PATH variable? – Caspian Ahlberg Dec 31 '19 at 11:58
0

Unfortunately, you can't easily run python3 cbc.py ... from a directory unless cbc.py is in that directory. But you can easily run cbc.py ... from any directory.

Edit cbc.py so that the first line is something like this:

#!/usr/bin/env python3

(ref Should I put #! (shebang) in Python scripts, and what form should it take?)

Make cbc.py executable:

chmod +x cbc.py

Now you should be able to run it without typing python3, but not yet from any directory:

./cbc.py ...

Next, edit ~/.bashrc so that the last line is:

export PATH:$PATH:...

and here, in place of ..., put the absolute path to the directory containing cbc.py.

A standard way of describing that is:

export PATH:$PATH:<absolute path to your script's directory>

For example (depending on your operating system):

export PATH:$PATH:/home/caspian/cbcdir

or:

# this path has a directory with a space in it's name, so it must go in quotes
export PATH:$PATH:'/Users/CaspianAhlberg/Documents/Cbc Project'

or:

# this path has a directory with a space and a single quote in it's name,
# so it must go in double-quotes
export PATH:$PATH:"/Users/CaspianAhlberg/Documents/Cbc's Project"

or, even more absurdly:

# you can also "escape" funny characters using \ instead of
# wrapping the entire string in quotes
export PATH:$PATH:/cygdrive/c/Users/Documents/Cbc\ Project

Long story short, don't put spaces nor quotes in directory nor file names.

Now start a new bash shell and try cbc.py ...

webb
  • 4,180
  • 1
  • 17
  • 26