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

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 ?

  • 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. – Cindy Jayakumar Jul 30 '15 at 8:42

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


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
        #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
        rkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment')
            v = str(win32api.RegQueryValueEx(rkey, varname)[0])
            v = win32api.ExpandEnvironmentStrings(v)
    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')

From my QGIS python console:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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