2

I have two Python scripts. The main script import the second script

test_credenziali.py (main script)

#!/usr/bin/env python
# coding=utf-8
# Copyleft Roberto Marzocchi - Gter srl Innovazione in Geomatica Gnss e Gis
import os,sys,shutil,re,glob, getopt
dirname0 =os.path.dirname(sys.argv[0]) 

#dirname = os.path.dirname(os.path.realpath(__file__)) #NameError: name '__file__' is not defined

dirname2= os.getcwd()
print('The path of the python script is ', dirname0 ) 
#print('The path of the python script is ', dirname ) 
print('The path of the python script is ', dirname2 ) 

# here i like to use the dirname variable
sys.path.insert(0, r'C:\Users\assis\Desktop\script_test')
from credenziali import *
print(host) 

credenziali.py

host='domain.it'

I save both script in the same folder (e.g on my desktop) C:\Users\assis\Desktop\script_test

and if I use the following line before the import of credenziali.py it works

sys.path.insert(0, r'C:\Users\assis\Desktop\script_test')

Nevertheless I like to use relative path in order to call the credenziali.py script. Using python normally it works or I can use the command

dirname = os.path.dirname(os.path.realpath(__file__))

to obtain the path of the main script.

Using the QGIS Python console it doesn't work.

1 Answer 1

1

What I believe is, (I may be wrong) that this happens because when you run a plugin the scripts are compiled and the files created, with extension .pyc are saved in the py_cache folder of your plugin.

But is Python supposed to be an interpreted language? Yes, but when you import a module, in this case os, that module is compiled in the .pyc file.

In the console I suppose that file is not accessible, or is temporary.

Let's hope a guru gives us better information.

1
  • Probably you are right. The strange thing is that using different console (e.g. the visualcode console for python) I can obtain the dirname without .pyc file Commented Sep 30, 2020 at 6:10

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.