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.