0

I am learning setuptools and I have a python package with this structure:

package1
     ├──setup.py
     ├── __init__.py
     ├── module1.py
     └── submodule1
         └── __init__.py

Note that I would like to place setup.py in the package1 folder itself, and make the distribution take the package1 files and name them package1? Then I would probably also need to ignore setup.py (avoid the file being distributed).

I am able to do this for now:

production
  ├──setup.py
  └──src
      └──package1
         ├── __init__.py
         ├── module1.py
         └── submodule1
             └── __init__.py

So why I would like this? I have a GitHub repository, and it's a python package. I would like to simply add the setup.py inside the root folder of the repository. In this way, someone cloning the repository could simply do python -m build. Makes sense or are there other ways? Thank you!

sinoroc
  • 18,409
  • 2
  • 39
  • 70
grexor
  • 109
  • 1
  • 5
  • Having the `setup.py` inside the importable package is a bad idea, I strongly recommend against it. What is wrong with the `src` layout that you have? With `src` layout you can perfectly clone and `python -m build`, this will work without issue, it is the preferred layout. What you might want to add though, is a `pyproject.toml` file. -- Also is there a particular reason you want to use *setuptools* specifically? There are alternatives (arguably easier to handle for beginners). – sinoroc Mar 31 '23 at 13:23
  • Thank you for the reply, was just for practicality purposes, having the sources in one folder and being able to build directly there would be great, no need to have `src/package`. – grexor Mar 31 '23 at 14:00
  • That does not make sense to me. With the project directory structure you have right now, you can build directly. I do not see why you would want to change and put everything at the root of the project. What you have right now, is the canonical project directory structure (with the `src` layout). -- Maybe you should edit your question to be more explicit about what you are trying to achieve. Maybe some kind of fake workflow of commands you would like to use and the result you expect. You mention `python -m build` and your project is already perfect for that. So I am confused... – sinoroc Mar 31 '23 at 14:51
  • Sorry if I was unclear, please see below my answer for a very direct and clear question (I hope:). Thank you – grexor Mar 31 '23 at 15:02
  • If you are open to another approach, I have found that using python-poetry for projects I wish to package up on pypi.org and such has made things way easier than writing my own setup.py. https://python-poetry.org/ – Andrew Allaire Mar 31 '23 at 15:13
  • Poetry would never allow the project directory structure asked for here, so we can completely forget about it. -- Spoiler: it is possible with *setuptools* but as I already said, it is bad practice, and would never recommend it unless there is a good reason for it. – sinoroc Mar 31 '23 at 15:21
  • Just out of curiosity then, how would you do it with setuptools? – grexor Mar 31 '23 at 15:25
  • https://stackoverflow.com/a/58429242 -- But once again, I recommend you not to do that, it has so many issues and red flags. – sinoroc Mar 31 '23 at 15:25
  • ok thank you, so you would recomment to have the `production/src/package1` structure or `production/package1` structure? what would be the best? I see for many packages having a `src` folder is pretty standard – grexor Mar 31 '23 at 15:29
  • Does this answer your question? [Setting package\_dir to ..?](https://stackoverflow.com/questions/17756742/setting-package-dir-to) – sinoroc Mar 31 '23 at 15:35
  • I recommend the `src` layout, as it is now. – sinoroc Mar 31 '23 at 16:05
  • @sinoroc Sure, when one uses poetry one should just stick with the directory structure which is common to poetry. I think of it like using python-black for style. I am happy to make arbitrary concessions to style or directory structure that enables tools that make my life easier. – Andrew Allaire Mar 31 '23 at 16:50

0 Answers0