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)
3
  • 1
    Have you try to print os.getcwd() where it's searching ?
    – SIGIS
    Commented Dec 15, 2017 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() Commented Dec 15, 2017 at 9:44
  • Update my answer
    – Fran Raga
    Commented Dec 15, 2017 at 10:43

1 Answer 1

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 and acknowledge you have read our privacy policy.

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