4

I’m trying to enhance QGIS's Profiletool plugin by adding DXF export functionnality. For this, I need to to import dxfwrite library. As it's installed on my computer, i added the following lines to plugin code:

import sys
sys.path.append("C:\Python27\Lib\site-packages")

With those short lines of code, I'm able to load dxfwrite. But it's hardcoded, so if the user does not have Python 2.7 but 3.4 (for example), library loading will probably fail because plugin will try to load dxfwrite from a non-existing repository. The same problem may happen if python path is not C:\Python27\....

I tried to put dxfwrite sources into plugin directory, but 1) with no results, 2) if results, it would link plugin to a specific version of dxfwrite and 3) if results, it would increase a lot plugin size.

So, how can I properly load dxfwrite without facing those problems ?

1
  • Welcome to GIS.SE. This sounds more like a pure Python problem, which would be better asked over at StackOverflow. I would suggest searching there first, because I had a similar issue a few weeks ago, and found the answer on there. Commented Jul 30, 2015 at 8:42

2 Answers 2

2

You will have to copy your python module in the plugin dir. and then you will get the path to your plugin with a small python script

import os
dirname, filename = os.path.split(os.path.abspath(__file__))
print "running from", dirname
print "file is", filename

This works with a C library, should be the same with a python module

0

Here's a solution. It uses a trick I recently had to learn, which I posted to System variable PATH overwritten in Qgis

It just finds the python that's installed on your computer. I would presume python.exe is in the system path, otherwise I don't know how you would expect to find it on someone else's machine. Anyway, here's the code. I hope it helps.

def which(program):
    import os
    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        #for path in os.environ["PATH"].split(os.pathsep):
        for path in getenv_system("PATH").split(os.pathsep):
            path = path.strip('"')
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file
    return None

def getenv_system(varname, default=''):
    import win32api
    import win32con
    v = default
    try:
        rkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment')
        try:
            v = str(win32api.RegQueryValueEx(rkey, varname)[0])
            v = win32api.ExpandEnvironmentStrings(v)
        except:
            pass
    finally:
        win32api.RegCloseKey(rkey)
    return v

import os
import sys
pythonexe = which('python.exe')
python_path, _ = os.path.split(pythonexe) 
lib_path = os.path.join(python_path,'Lib','site-packages')
sys.path.append(lib_path)

From my QGIS python console:

>>>lib_path
'C:\\Python34\\Lib\\site-packages'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.