10

I am working on a python project which uses pythonnet with several C# dll's as dependencies. As I do not want to push the dll's to the git repository I adapted the .gitignore file. However, now Poetry does not include the dll's into the python package.

Is there a way to force Poetry to ignore the .gitignore?

jodá
  • 342
  • 7
  • 25

3 Answers3

7

Yes, the .gitignore just serves as the default-settings for poetrys inclusion/exclusion list. You can configure it manually with the include field, which is documented here.

In your case, you just need to specify the dll folder that you are excluding in your gitignore:

.gitignore

# ...
src/dlls

pyproject.toml

[tool.poetry]
# ...
include = [
    "src/dlls/some.dll",  # listing files explicitly
    "src/dlls/*",         # all files in "src/dlls"
    "src/dlls/**/*",      # all files in "src/dlls" and any subfolders
    "src/dlls/**/*.dll",  # all files in "src/dlls" and any subfolders
                          # with the file ending ".dll"
]
Arne
  • 17,706
  • 5
  • 83
  • 99
  • 1
    Thank you @Arne. By the way: I obtained that an exclude does not work for the `__pycache__` folder. However this is not a big deal as I will handle it anyway with the .gitignore file. – jodá Mar 02 '21 at 14:06
  • happy to help. good to know about the `__pycache__`, I've been lucky enough with all my project that having just the .gitignore settings was enough. – Arne Mar 02 '21 at 14:49
  • This doesn't work for me. Even when I explicitly specify to include Git ignored files, I still don't see them in my build artifact. I'm new to Poetry and this is pretty annoying. – Psycho Punch Apr 29 '21 at 16:08
  • @PsychoPunch hard to say what might be wrong without seeing your project structure. Just to be sure, absolute paths and starting with a `..` won't work, the source files need to be reachable relative to the pyproject.toml file. – Arne Apr 30 '21 at 14:19
  • @Arne I tried it with a fresh project, but I got the same result. Would you be able to quickly create a demo where it works? It looks to me like the `include` property does not override the `exclude`... – Psycho Punch Apr 30 '21 at 19:32
  • 1
    @PsychoPunch [here](https://gist.github.com/a-recknagel/8ae4f94e4d319d25051099771fb1006a). Is that what you had it mind? Note that I'm only overriding the _default_ excludes, not explicit ones. – Arne May 07 '21 at 10:37
  • @Ame Thank you very much! I think what's missing is the `*` after `src/dlls`. Without that, Poetry will continue ignoring the files. – Psycho Punch May 08 '21 at 22:49
  • whoops, thanks for finding and correcting that. – Arne May 10 '21 at 15:23
4

Just to expand on the answer above, for directories that have deeper structure, use */** to recursively add contents from the Git ignored directory.

pyproject.toml

[tool.poetry]
# other configuration here
include = ["src/extra/*/**"]
Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
  • 1
    I think you have it backwards. The code uses [Path.glob()](https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob), which says that `**` means "this directory and all subdirectories, recursively," so to get all files, you'd want `"src/extra/**/*"`. You can try `len([x for x in Path('/path/to/project').glob('src/extra/*/**') if x.is_file()])`, which for me shows `0` files. – paulie4 Nov 10 '22 at 18:21
  • The correction suggested by @paulie4 made it worked for me. I have a directory with generated Python code that I don't want to commit to Git (`generated` was added to .gitignore), but still be packaged by poetry. `include = ["generated/**/*"]` did what I wanted. – Erik Finnman Apr 14 '23 at 08:41
4

Surprisingly none of the above answers worked for me. I also had a deeper structure where I wanted files not to be present in the repository, but to be recursively included upon building/publishing. The following globbing pattern worked out for me:

.gitignore

my_folder/

pyproject.toml

[tool.poetry]
...
include = ["**/my_folder/**/*.py"]
faemmi
  • 201
  • 1
  • 4