0

My end goal is simple: Store a set of .py scripts on my Linux system such that I can:

  • Run them from the command line without using /Python [name]
  • Edit them easily

I can fix the first issue by storing the scripts in /usr/local/bin/

This causes the second issue, because now when I attempt to open the script for editing purposes, I am denied permission. I can partially fix this issue by using /sudo vi [name], which is a disappointing fix because who wants to code in vi?

So I either want the ability to read and write scripts in /usr/local/bin/, or I need the ability to store the scripts elsewhere yet still have them accessible from the command line.

My current (broken*) fix is to store them in a personal directory located somewhere in my home directory, which is added to my path. I have heard this is terrible style however. Thoughts?

*I am currently having trouble adding a personal folder to the PATH. This is content for another question however .

SirLich
  • 79
  • 1
  • 12

3 Answers3

1

I would suggest you write command line alias to your ~/.bash or ~/.bash_profile, similar to the following codes

# script path
alias python_script_1="PATH_TO_PYTHON_SCRIPT_1"
# run script
alias run_python_script_1="python PATH_TO_PYTHON_SCRIPT_1"

If there are multiple python files, try to generate the file using a Python script.

Neo X
  • 947
  • 7
  • 9
  • The set of files are not aliases though. They are actual .py scripts that run Python. I am writing a simple file encrypt which I want to use from the command line like: lichcrypt – SirLich Mar 02 '17 at 01:18
  • Then you can try to write simple shell functions as wrappers, see http://stackoverflow.com/questions/7131670/make-a-bash-alias-that-takes-a-parameter. It will enable you to store and edit python scripts independent of `bin` dir (which would be better to be kept clean). – Neo X Mar 02 '17 at 01:33
1

There is another alternative, place your python files in /home/{your_name}/bin . /home/{your_name}/bin is typically included in distro's default paths, which bypasses the issue you are having regarding being unable to edit your path.

If that is not an option you could edit the permissions of the directory /usr/bin, but that is a bad idea from a security standpoint. (SRC: https://unix.stackexchange.com/questions/37724/permissions-ownership-of-usr-local-bin#37726 )

Another option you have is to give your default program which you edit files with sudo permissions. So you could do /sudo xdg-open file_name if the default way you open python files is the editor, or you could do /sudo editor_name file_name if it is an editor which by default opens another the file in its gui.

Ultimately though, fixing the issue with your path and editing it is probably your best solution to avoid giving unnecessary permission to files.

Ninja_Coder
  • 264
  • 1
  • 9
0

Try this:

  • Add a shebang at the top of your script which tells how to execute your script,like this:

    #!/usr/bin/python
    
  • Give the script x permissions.

    chmod +x test.py
    
  • Add the folder to your PATH,you can add this to your .bashrc

    export PATH=/directory/pythonscript:$PATH
    

Or

cd /usr/bin
ln -s ~/path/to/script
McGrady
  • 10,869
  • 13
  • 47
  • 69