0

I am wondering how to make an entry point to call a python script, similar to black.
e.g.:
black my_script.py

Say I have a python file called fix_newline.py.

Instead of calling python fix_newline.py path/to/my_script.py in the directory of fix_newline.py, I'd like to assign the name fix_newline to python path/to/fix_newline.py.

The ultimate goal is to call fix_newline from anywhere in my directory tree, as long as I am in the same environment (e.g. ~/.bash_profile).

irahorecka
  • 1,447
  • 8
  • 25
  • 3
    build a proper python package with a setup.py etc. ? – Chris_Rands Sep 20 '20 at 21:01
  • OK, this is the path I am looking to go (eventually want to package script). Could you point me to a useful resource? – irahorecka Sep 20 '20 at 21:05
  • 1
    https://packaging.python.org/tutorials/packaging-projects/ https://packaging.python.org/ etc. – Chris_Rands Sep 20 '20 at 21:06
  • 1
    I also like RealPython: https://realpython.com/pypi-publish-python-package/ and https://realpython.com/lessons/building-your-python-package/ – wcarhart Sep 20 '20 at 21:07
  • One useful thing you can do, by the way, is run `type black`. If `black` were an alias, the `type` command will tell you that. Instead, it's an executable entry-point wrapper; _that's_ what you should be trying to create (or, rather, letting the packaging tools create on your behalf). – Charles Duffy Sep 20 '20 at 21:13
  • 1
    @irahorecka, re: the tools to use to do this job right, see [explain python entry points](https://stackoverflow.com/questions/774824/explain-python-entry-points) – Charles Duffy Sep 20 '20 at 21:19
  • @irahorecka, ...but no, what that creates is **not** an alias. "alias" is a specific technical term with a very limited meaning -- it refers only to places where the shell is configured to perform prefix substitution. `pip` is not an alias. `black` is not an alias. – Charles Duffy Sep 20 '20 at 21:19
  • ...and answers should be added **as answers**, not edited into questions. – Charles Duffy Sep 20 '20 at 21:20
  • @CharlesDuffy, let me change to entry point – irahorecka Sep 20 '20 at 21:21

3 Answers3

1

I believe I found a way to make an alias via packaging to PyPI as suggested by @Chris_Rands and link provided by @CharlesDuffy:

setup(
    ...,
    entry_points={"console_scripts": ["realpython=reader.__main__:main"]},
)

The entry_point kwarg in setup function in setup.py should be the trick.

irahorecka
  • 1,447
  • 8
  • 25
0
  1. Add path/to to your PATH variable .bash_profile. (If you have a lot of scripts, consider installing them in a fixed location like ~/bin/, so that you don't add a lot of unnecessary directories to your PATH.

  2. Make sure you script is executable and has an appropriate shebang.

  3. Drop the .py from the script name.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    possible ok in the short term but this feels like a path to messy package management – Chris_Rands Sep 20 '20 at 21:04
  • 1
    You don't need full-blown package management. One option is to simply create a symlink in `~/bin/` to wherever your script lives. – chepner Sep 20 '20 at 21:35
  • 1
    (My answer was assuming the OP didn't *want* to create an actual package, but merely run a script from some working directory.) – chepner Sep 20 '20 at 21:36
0

You could also use a Bash alias for this: https://tldp.org/LDP/abs/html/aliases.html

Make sure you use absolute path to python and fix_newline.py on your machine:

alias fix_newline='/path/to/your/python /path/to/fix_newline.py'

Then you can use with:

fix_newline file.txt

If you want this alias to be permanent make sure to put this configuration in you ~/.bashrc file: https://tldp.org/LDP/abs/html/sample-bashrc.html

wcarhart
  • 2,685
  • 1
  • 23
  • 44
  • Please don't link to the ABS -- the freenode #bash channel denizens wrote the [BashGuide](https://mywiki.wooledge.org/BashGuide/CommandsAndArguments#Aliases) because we were tired of needing to teach people to unlearn bad habits they picked up there. Also consider the [official manual](https://www.gnu.org/software/bash/manual/html_node/Aliases.html) as a canonical source that's reliably updated whenever bash is. – Charles Duffy Sep 20 '20 at 21:11
  • (Also, `black`, `pip`, etc. which the OP wants to emulate aren't aliases at all) – Charles Duffy Sep 20 '20 at 21:12
  • ...that's important, because `~/.bashrc` isn't run by noninteractive shells, so if your editor or another C executable runs a shell command, it won't see aliases, so it won't be able to find the desired tool. (And even if it _does_ read `.bashrc`, aliases are turned off by default in noninteractive shells!) – Charles Duffy Sep 20 '20 at 21:18