3

Situation: I have a Python file called 'Hello.py' and another Python file called 'File1.py'. Hello.py imports File1.py.

Hello.py code is:

print 'hello people'
import File1
File1

File1.py code is:

print 'hello world'

The files are located:

My files is located C:\Program Files\QGIS 2.18\PyPack\

Using 'OSGeo4W.bat' provided in the QGIS directory, I execute the following commands:

C:\Program Files\QGIS 2.18>cd C:\Program Files\QGIS 2.18\PyPack

python Hello.py

And the output is:

hello people
hello world

What do I type in this window to run Hello.py, which gives the output? enter image description here

I have tried:

1 - dragging the 'Hello.py' file into the QGIS Python console

2 - the following command:

execfile(u'C:\Program Files\QGIS 2.18\PyPack\Hello.py'.encode('mbcs'))

3 - I tried to open 'Hello.py' in the QGIS python editor and run it with the same output.

enter image description here

And the outputs were:

hello people 
Traceback (most recent call last): 
File "<input>", line 1, in <module> 
File "C:\Program Files\QGIS 2.18\PyPack\Hello.py", line 2, in <module> 
import File1 
File "C:/PROGRA~1/QGIS2~1.18/apps/qgis/./python\qgis\utils.py", line 607, in _import 
mod = _builtin_import(name, globals, locals, fromlist, level) 
ImportError: No module named File1 
mgri
  • 16,159
  • 6
  • 47
  • 80
LHo
  • 509
  • 5
  • 19

2 Answers2

5

1) Just drag the python file to frame of python in QGIS.
2) Another way:

execfile(u'C:/hi.py'.encode('mbcs'))

To import python modules while working on QGIS, write below lines:

import sys 
sys.path 
sys.path.append("C:/PyPack") #Add this folder to python environment path
import File1 
Shiko
  • 2,883
  • 13
  • 22
  • That's great, thank you. Is there also a way to call it by typing into the console? – LHo Apr 12 '17 at 04:08
  • I updated the question to include imported files. Would you mind please updating your answer if possible? – LHo Apr 12 '17 at 04:24
  • @Joe answer updated – Shiko Apr 12 '17 at 04:27
  • I get this output: execfile(u'C:\Program Files\QGIS 2.18\PyPack\Hello.py'.encode('mbcs')) hello people Traceback (most recent call last): File "", line 1, in File "C:\Program Files\QGIS 2.18\PyPack\Hello.py", line 2, in import File1 File "C:/PROGRA~1/QGIS2~1.18/apps/qgis/./python\qgis\utils.py", line 607, in _import mod = _builtin_import(name, globals, locals, fromlist, level) ImportError: No module named File1 ...... I might need to update the one of the python files to make the import work in QGIS Python console? – LHo Apr 12 '17 at 04:30
  • Have you tested it outside of QGIS ? was is the same case when you dragged the file ? – Shiko Apr 12 '17 at 04:33
  • Shiko Yes I have. Yes it was the same result when just dragging. I used OSGeo4W.bat, typing in 'python Hello.py' and it works fine. I think there is an issue with QGIS properly importing other files. Might be because it is trying to import the file from some default location? – LHo Apr 12 '17 at 04:35
  • Run QGIS as Admin – Shiko Apr 12 '17 at 04:37
  • Ran QGIS as admin and tried the command and the output was the same. Interestingly I can't drag files into the frame when QGIS is opened as Admin.. – LHo Apr 12 '17 at 04:48
  • Try to load your python file using the QGIS python editor then run and let me know. – Shiko Apr 12 '17 at 04:52
  • Same output. See end of updated question – LHo Apr 12 '17 at 04:56
  • Did you check this link http://gis.stackexchange.com/questions/129959/problem-with-import-qgis-core-when-writing-a-stand-alone-pyqgis-script – Shiko Apr 12 '17 at 05:02
  • Thank you for providing the link. I'll review and try it and let you know what happens. – LHo Apr 12 '17 at 05:06
0

Python 2 builtin function "execfile" was removed in Python 3.0, now you can use:

exec(open(r"C:\Program Files\QGIS 2.18\PyPack\Hello.py".encode('utf-8')).read())

On this line, we open the .py file (assuming it was saved with the usual "utf-8" encoding), then get the content as a string, and finally run the code in the Qgis python console line by line.