3

Given I have a folder 'foo' with some .dll files which needs to be on PATH at runtime of my python script 'setup.py' Is it possible to put code inside my setup.py script that will put 'foo' folder on PATH before executing the reset of the code which requires the .dll files to run?

So far I have tried this:

import sys
import os
foo = os.path.abspath('dependencies/foo')
if sys.path[1]!= foo:
    print('Adding foo to PATH:')
    sys.path.insert(1, foo)
    print sys.path
else:
    print('foo is already on PATH!')

import lib_which_requires_dll_from_foo
...

but it has no effect, although sys.path shows that 'foo' is on PATH - or am I mistaken and it's not that PATH which it should be on.

My setup.py script works if I put 'foo' on PATH with set PATH=C:\my_path_to\foo;%PATH%)

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Wlad
  • 2,866
  • 3
  • 28
  • 42
  • 3
    `sys.path` is used to find python modules. Why do you expect it to affect dll loading? – spectras Oct 25 '16 at 12:09
  • Does your script work if you put the .dll files into the current directory that you launch the script from? I don't use Windows, but IIRC, .dll files generally live in system32. – PM 2Ring Oct 25 '16 at 12:19
  • @spectras I am relative new to python ... obviously my expection was wrong :( If `sys.path` is the wrong method which one should I use instead? – Wlad Oct 25 '16 at 12:36
  • @PM2Ring Yes, it work. So what does that mean? Would it be ok to just add the files to PYTHONPATH? – Wlad Oct 25 '16 at 12:36
  • No, the shell's `PYTHONPATH` and Python's `sys.path` are used to find _Python_ files. Those `.dll` files should go somewhere that your OS will look when it's trying to find a DLL, so (as I said before) either the current directory or WINDOWS/system32. FWIW, [here](http://stackoverflow.com/a/26878137/4014959) is some Python code I wrote a couple of years ago that uses an external library, it works on Windows and on Linux. – PM 2Ring Oct 25 '16 at 12:49
  • @PM2Ring Thanks for the link. If I understand the code right it requires to import `ctypes` and then use `cdll.LibraryLoader`? 'Name' parameter of LibraryLoader can bee a folder name, too or must it be a single .dll? In my 'foo' folder I have 6 .dll and 3 .lib files. – Wlad Oct 25 '16 at 14:20
  • I don't think you can give a pathname to `cdll.LibraryLoader`; at least, all the examples in [the docs](https://docs.python.org/3/library/ctypes.html#loading-dynamic-link-libraries) just give the base name of the library. – PM 2Ring Oct 25 '16 at 15:25
  • 2
    I solved my problem with `os.environ['PATH']`. Here's the code: `import sys import os foo = os.path.abspath('dependencies/foo') if os.environ['PATH'].find(foo): print('Adding foo to PATH:') os.environ['PATH'] = foo + ";" + os.environ['PATH'] print('Done!') print os.environ['PATH'] else: print('foo is already on PATH!')` But just wonder why it is `if os.environ ...` instead of as I expected `if not os.environ ...` – Wlad Oct 25 '16 at 15:51

1 Answers1

0
import os
FOO = os.path.abspath('path_to_my/FOO')
os.environ['PATH'] = FOO + ";" + os.environ['PATH']

I removed the if condition because PATH is altered temporary only - not permanently, thus there is no need to check it first.

NOTE: that will propably work on Windows only.


This answer was posted as an edit to the question How to put a folder on PATH with Python? by the OP Wlad under CC BY-SA 3.0.

vvvvv
  • 25,404
  • 19
  • 49
  • 81