1

I have a user script which is located in C:\Users\'myusername'\.qgis2\processing\scripts. enter image description here

I call script "plotWLayer1" containing the line

import funcPlot

User script funcPlot is also located in the same path but when I execute the script Qgis logs the following:

No module name funcPlot

I have tried adding the path using,

import os
import sys
sys.path.append(os.getcwd())

with the same result. What am I doing wrong? My intention is to give these scripts to fellow users within my company but if I hard code sys.append() the script would have to modified per user.

Update 1: For the moment I am using the following code under the assumption that only the username for different users in my company would change but any suggestions would still be welcomed.

import getpass
username = getpass.getuser()
import sys
exePath='C:\\Users\\'+username+'\.qgis2\\processing\\scripts'
sys.append(exePath)
  • 1
    Have you try to print os.getcwd() where it's searching ? – SIGIS Dec 15 '17 at 9:34
  • I did but I am not sure where it is output. I checked the log and I found some execution in C:\OSGeo4wShell\..., without the output of print() – Stephen Jacob Dec 15 '17 at 9:44
  • Update my answer – Fran Raga Dec 15 '17 at 10:43
2

For import "funcPlot" in you "plotWLayer1" you can use this script:

import imp, os.path
from qgis.core import QgsMessageLog

def import_(filename):
    (path, name) = os.path.split(filename)
    (name, ext) = os.path.splitext(name)
    print name
    print path

    (file, filename, data) = imp.find_module(name, [path])
    return imp.load_module(name, file, filename, data)

funcPlot = import_("C:\Users\'myusername'\.qgis2\processing\scripts\\funcPlot.py")

And if funcPlot.py have a function for example def myFunction()

Call it using funcPlot.myFunction()

Update:

Other way is using exePath=os.path.expanduser('~') +"\\.qgis2\\processing\\scripts" because is not necesary that all user have a C driver letter

I hope this helps you

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.