1

My package needs external libraries (with .dll extension) from a directory located, for example, in "C:\Program Files\NVIDIA Corporation\GSTPlugin" . This path can change, it depends on the computer. It could be in Programs Files. During the installation from PyPi using pip install my_package I want Python to recognize these libraries, as I want to use _NvGSTPlugin.dll.

Any ideas how to implement this?

I tried to include in the setup.py file a script as a post-install, but as I use a wheel file it doesn't work (because there is no setup.py in a wheel).

A solution is to add a .pth with the path of these dlls in the src directory as this link setup.py: installing just a pth file? shows. The problem is that I dont know where everyone have located these dlls. I could know looking in the windows register.

  • Something is not clear to me. Does your library only need to have read access to this *external thing* (that is maybe located at `C:\Users\%USERPROFILE%\Desktop`) or does your library itself write this *external thing* at that location to begin with? -- What is a `.pht` file? Is it relevant to know what it is to solve your question? Or should it be assumed to be "*some external file*" and we do not need to concern ourselves with what it is exactly? -- Why the "package site" is mentioned in the title but not in the question? – sinoroc Feb 27 '23 at 16:13
  • 1
    @sinoroc I'm sure the OP means [.pth files](https://docs.python.org/3/library/sys_path_init.html#pth-files). Just didn't recognize the wotd "path" in `.pth`. – phd Feb 27 '23 at 16:22
  • @phd That is what I thought as well reading only the title, that it is about `.pth` file. But then the question itself throws me off, since it does not mention `.pth` nor `site-packages`. -- @Manuel Are you asking about `.pth` file as in the following document? https://docs.python.org/3/library/site.html?highlight=.pth – sinoroc Feb 27 '23 at 16:30
  • 1
    @sinoroc I have several .dll in my Desktop. My package needs them to work. So, if I install it using pip, I have to point manually to this direction. To achieve this, I create a file .pth with the path of the folder where my .dll are and I save it in the Python39\site-packages\ folder. These .dlls are external and they are downloaded from another page. I can read the dll because I use the pythonnet package. – Manuel Sánchez García Feb 28 '23 at 07:57
  • @Manuel I recommend you add such details in the question itself, better than in the comments. – sinoroc Feb 28 '23 at 08:54
  • 1
    How are you using these .dlls in your code? – AKX Feb 28 '23 at 08:59
  • To answer the question of how to package a `.pth` file with your project, so that it is automatically placed in `site-packages` when the project is *pip*-installed, I guess that I would follow this answer from a prominent member of PyPA and Python packaging in general: https://stackoverflow.com/a/71137790 – sinoroc Feb 28 '23 at 10:53
  • @sinoroc Thanks for your help. The link solves partially my problem. The path that is inside the .pth could change in each computer. My original idea was to execute a .py during the installation to look into the windows register, find the right path there and crate the .pth file. I am also editing the question with all these extra information, as my first post was quite confusing. – Manuel Sánchez García Feb 28 '23 at 12:24
  • @AKX I am not really sure about what I am doing. I have installed the pythonnet package. I import the dll, as if it was a .py package or module and it works. – Manuel Sánchez García Feb 28 '23 at 12:30
  • I see. Well you can't really dynamically create the content of the `.pth` file at installation-time. There is no real guarantee that `setup.py` is going to run, you do not have much control over this. So you would need to write the content of `.pth` file at run-time, and I am far from sure that it is acceptable to write anything in `site-packages` at run-time by-passing *pip* (because then the `.pth` is not un-installed properly). Maybe package a blank `.pth` file and overwrite its content at run-time. -- Are you sure you need a `.pth` file? – sinoroc Feb 28 '23 at 12:30
  • @sinoroc The .pth solution was the only idea I had at first. If there is another way to point the dlls directory that is automatically done when you install the package it would be perfect. – Manuel Sánchez García Feb 28 '23 at 12:38
  • The thing is that you do not clearly explain in your question what you are actually trying to do, what is really blocking you. It feels like an [XY Problem](https://en.wikipedia.org/wiki/XY_problem), you ask for something (a `.pth` file) but actually you need something else (which is still not clear, something with DLLs). I would recommend you rewrite your question with maybe links to the doc of the libraries that say that DLLs are needed. Are these DLLs Python extensions? Maybe write a [mre] as well. Add links to docs. – sinoroc Feb 28 '23 at 12:42

1 Answers1

0

Right, so if the issue is being able to use an arbitrary directory full of .NET assembly DLLs with PythonNet, which adds its own magic importer that allows loading .NET assemblies as if they were Python modules, this is actually documented in the PythonNet wiki and .pth files don't really play into this at all (other than being a way to "autoload" things onto sys.path).

Assuming you don't want the user to have to have the DLLs on your desktop, but instead in e.g. a NET_Assemblies directory in your program's directory, let's adapt that a bit:

import clr
import sys
import os

assembly_path = os.path.join(os.path.dirname(__file__), "NET_Assemblies")
assert os.path.isdir(assembly_path)  # sanity check
sys.path.append(assembly_path)

clr.AddReference("MyDLLName")
from NamespaceInMyDLL import ...

Of course, if you have a way of knowing where the GSTPlugin directory could be, even better.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • thats basically what I am doing, but I dont know where the DLLs are placed. I can predict where most of the users will have it and add those path to the .pth, but not always will work. – Manuel Sánchez García Mar 01 '23 at 11:32
  • Again, you shouldn't touch `.pth` files, since there's no need to. If you can't always predict where the DLLs are, show an error message telling the user to put the DLLs where you can always tell them to be in..? – AKX Mar 01 '23 at 11:56