0

I am trying to load a dll in python, and can only do so if I enter the absolute path. I would like to use a relative path, or environment variables. The only thing that works is if I specify the exact path (C:...) I even tried to get the dll to build directly in the same folder as the py file, it still didn't work.
What I have:

MY_DLL = r'c:\full_path\output\Win32\Debug\my.dll'
#MY_DLL = r'my.dll'   #this doesn't work but it is what I want
#MY_DLL = r'$(env_var)\dir\output\$(Platform)\$(Configuration)\my.dll'   #this doesn't work either but would be good too     

Help ?

Thalia
  • 13,637
  • 22
  • 96
  • 190
  • 1
    could you use `os.path.expandvars`? – mgilson Apr 13 '12 at 19:57
  • I did from os import path MY_DLL = os.path.expandvars(r'$(env_var)\dir\output\$(Platform)\$(Configuration)\my.dll') But it showed that it is trying to load exactly what I put after the = and then NameError: 'os' is not defined. Sorry, clueless.. Did I put it in right ? – Thalia Apr 13 '12 at 20:07
  • 1
    try `import os.path` instead of `from os import path`. You also might need to change `$(env_var)` to `${env_var}` -- I'm not sure. The name error is because you didn't import os, you imported path from os. (the way you did it, os.path is known in your namespace as just path) – mgilson Apr 13 '12 at 20:09
  • Thank you, it worked - but didn't expand the $(Configuration) and $(Platform). It is a lot more flexible than before. How do I mark that you have answered my question ? – Thalia Apr 13 '12 at 20:29
  • I've posted an answer below -- You can click on the checkbutton below the number with the arrows above and below. To expand Configuration and Platform, you should use the `${}` syntax (not `$()`) and they should expand too (as long as they're set in your environment). – mgilson Apr 13 '12 at 20:33

1 Answers1

1

I don't know about cdll on windows or really much about ctypes in general, however, you can manipulate paths quite easily using os.path:

import os.path
p1="path.dll"
print (os.path.abspath(p1))
p2="${env_var}/path.dll"  #Make sure you set env_var in the calling environment...Otherwise it won't be expanded...
print (os.path.expandvars(p2))
mgilson
  • 300,191
  • 65
  • 633
  • 696