2

I have a python command line app I want to package for pypi. I have followed this: https://packaging.python.org/en/latest/tutorials/packaging-projects/. The key point being that the package files are located @ <package>\src\<package>. The goal is to obtain a .exe that would be installed into the pythonXX\Scripts directory with pip. This line added to pyproject.toml does just that:

[project.scripts]

<exe_basename> = '<package>.<script_module>:main'

First, the only information I can find on this behavior is here: Specifying command line scripts in pyproject.toml. And, while not stated there, I can confirm this also works for Hatchling. How it works is opaque to me. The exe file that is installed in pythonXX\Scripts IS NOT part of distribution *.tar.gz file so I assume pip generates it during the installation. Can anyone confirm this and/or point me to additional information on how this works?

Second, I'm not able to make this work for <script_module> not part of <package>. Why would I not want it part of the package? Because in order to import other modules from the package the <script_module> does this: import <package>.<module_x>

And because my development environment is pyCharm, I can't configure my sys.path during development so that it recognizes the package in the <project>\src directory (sys.path is set to <project>\src\<project>, one directory below where is would recognize the package). So, I really want to do this:

[project.scripts]

<exe_basename> = '<script_module>:main'

where Hatchling/pip knows to look for this in the <project>\src, NOT <project>\src\<project>.

Any clues and crumbs to follow appreciated.

I was hoping the [project.scripts] allowed the <script_module> to be located some place other that embedded along with the package.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • You seem to be making `sys.path` manipulations... this is usually a red flag for me. Your question is not clear to me. Why is `script_module` not part of `package`? Your explanation about imports does not make sense to me. I recommend you edit your question to show a [mre] including the project directory structure. -- Yes, the `.exe` file is generated automatically (by *pip*) during installation. – sinoroc Mar 07 '23 at 15:22
  • I also do not understand how PyCharm is relevant here. Your question is a bit hard to decypher, but I sense that maybe it could be because you are not installing your project as "editable". Could it be? – sinoroc Mar 07 '23 at 15:40
  • Thanks for the reply. And yes there are sys.path manipulations but that seems to be the point of 'editable' mode. Isn't it? – NW_BlackDog Mar 17 '23 at 20:08
  • An editable installation does not necessarily do `sys.path` manipulations. If it does, it is as a means to an end, Making `sys.path` manipulations is not the point of editable installations. But it is true, it seems some implementation of editable installations rely on `sys.path` modification, but again that is not the point. I recommend you rely on editable installations instead of doing `sys.path` modifications yourself. -- Anyway, again: Why is `script_module` not part of `package`? I recommend your rewrite your question to be more clear and straight to the point. Preferably with [mre]. – sinoroc Mar 17 '23 at 23:14

1 Answers1

2

You can leverage the Forced Inclusion feature of Hatch to move your script into the nested package directory.

So let's say your script is called cli.py and it has a function main. Let's further assume you'd like the executable to be called executive, and your project is called project_name.

You would add the following lines to your pyproject.toml:

[tool.hatch.build.target.sdist.force-include]
"src/cli.py" = "src/project_name/cli.py"

[project.scripts]
executive = "project_name.cli:main"

By the way, once your package is installed having a file nested within the package which does import package.module... is fine, because it's highly unlikely that you're importing your script within the package itself. There is no need to move it outside for that reason alone.

Here's a reference on Console Scripts that you might find useful.

aqua
  • 3,269
  • 28
  • 41