Python: Refer a properties file from different location

Hi All,
I'm having a python script: test.py in /path/to/script/test.py
I'm using a properties file: test_properties.py (it is having values as dictionary{}) which is in same DIR as the script.

Sample Properties file:

params = {
'target_db'     :     'a1_db'
'src_db'           :      'src1_db'
'countryCode'   :    'cc'
'date_1'    : 'start_date'
'date_2'   : 'end_date'
}

My sample code:
==============

from test_properties import *

It is working fine.
//Issue//
Now I'm keeping the properties file in: /path/to/config DIR and referring it in my script.
I have tried:
==========

fileName1=os.path.join('/path/to/config','test_properties.py')
from fileName1 import *

It is giving error:
==============

ImportError: No module named fileName1

I'm using python version 2.6 (default with Linux as of now in all Non-PROD serves) and I have requested Admin to to upgrade/install python 3.4+

Please help.

Thanks,
Saptarshi

Hmm.
Please post the output of your directory:

ls -la /path/to/config

It wants the bare module name - not a pathname.
Set the module search path instead! Like in
Expand Python Search Path to Other Source - Stack Overflow

Sure Neo. The o/p as below:

Old setup:

-rw-r----- 1 dev_user1 dev_staff   76 Nov 15 09:09 test.py
-rw-r----- 1 dev_user1 dev_staff 2382 Nov 15 09:11 test_properties.py
-rw-r----- 1 dev_user1 dev_staff 1478 Nov 15 09:11 test_properties.pyc

New setup:

-rw-r----- 1 dev_user1 dev_staff   76 Nov 15 09:09 test.py
-rw-r----- 1 dev_user1 dev_staff 2382 Nov 15 09:11 test_properties.py
-rw-r----- 1 dev_user1 dev_staff 1478 Nov 15 09:11 test_properties.pyc
drwxr-x--- 2 dev_user1 dev_staff 4096 Nov 15 09:12 config

inside ./config folder:

-rw-r----- 1 dev_user1 dev_staff 2382 Nov 15 09:13 test_properties.py

--- Post updated at 05:20 AM ---

Sure MadeInGermany,

Checking the link and I'll try the solution posted there.

~ Sapta

So, I guess you are sure this is not a permission issue since only dev_user1 user and dev_staff group can read this file?

Seems like a wild shot... just checking in between working on mysql query testing.

Yeah, no permission issue. Just I'm unable to refer the config file if I moved it from the script DIR to any other DIR.

Actually I'm planning to have a "standard folder" structure for production.
so,
all scripts will be in "scripts" folder
all config will be in "config" folder
all SQLs will be in "sqls" folder
all reports will be in "output" folder
and so on...

Then I did that and tried to test the scripts again and failing as the scripts were not able to refer the config files from different dir.

Did you try hard coding the full path into your code for testing?

For example:

from /path/to/config/test_properties.py import  *

That works fine right?

Hi Neo...
No.
Direct absolute paths do not work from import, however there is a similar effect using execfile(...) as the OP is using Python 2.6.x...
(... exec(open(...).read()) for 3.x.x to the current 3.8.0...)
An example for a maths library I an developing Python 2.4.6 for the AMIGA which does not have the builtin...

Last login: Fri Nov 15 10:59:24 on ttys000
AMIGA:amiga~> python
Python 2.7.10 (default, Feb 22 2019, 21:55:15) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> execfile("/Users/amiga/Desktop/maths.py")
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'cos', 'dft', 'dwt', 'e', 'exp', 'fp_power', 'pi', 'sin', 'sqrt', 'sys', 'tan', 'test', 'truncate']
>>> _

The OP will probably have to look at the builtin 'sys' and import that:

>>> import sys
>>> help(sys.path)

For more info on adding the path to the Python environment...
I no longer have version 2.6.x to test with...

1 Like

Hi Neo, Wisecracker;

I have tried this. Full path is not working.

import os, sys
#from ../config/test_properties import *
#sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/path/to/config")  ## <----- This is NOT working
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/./config")    ## <----- This is working
from test_properties import *
params=paramsOriginal
print(params)

So for actual implementation, I need do something like /../../config (Maybe!?).

Any suggestion is most welcome.

Cheers,
Sapta.

Try the full path without this built-in prefix:

os.path.realpath(__file__)

Like this:

sys.path.append("", "/path/to/config") 

Does something like that work?

Might be a good idea to print this, when debugging:

os.path.realpath(__file__)
1 Like

Apologies for any typos...
There is no better use than a working example...
Longhand using OSX 10.14.6, default bash terminal:

Last login: Mon Nov 18 09:29:24 on ttys000
AMIGA:amiga~> # My maths library is on the Desktop
AMIGA:amiga~> # I am currently in my ${HOME}.
AMIGA:amiga~> 
AMIGA:amiga~> python
Python 2.7.10 (default, Feb 22 2019, 21:55:15) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("/Users/amiga/Desktop/")
>>> # My maths library is in the above directory!
... 
>>> from maths import *
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'cos', 'dft', 'dwt', 'e', 'exp', 'fp_power', 'pi', 'sin', 'sqrt', 'sys', 'tan', 'test', 'truncate']
>>> exit()
AMIGA:amiga~> 
AMIGA:amiga~> # Back to the shell.
AMIGA:amiga~> _

Read my previous post again.
A similar effect of from module import * is execfile("/full/path/to/module[.py]") if you are not sure about how to add diretories to a Python PATH.
Also IF you are importing 'os' it already has 'sys' so there is no need to import 'sys'; this is true for all versions of Python as far back as 1.4.0:

AMIGA:amiga~> python3.8
Python 3.8.0rc1 (v3.8.0rc1:34214de6ab, Oct  1 2019, 12:56:49) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import os
>>> 
>>> sys=os.sys
>>> 
>>> sys.path.append("/Users/amiga/Desktop/")
>>> 
>>> from maths import *
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'cos', 'dft', 'dwt', 'e', 'exp', 'fp_power', 'os', 'pi', 'sin', 'sqrt', 'sys', 'tan', 'test', 'truncate']
>>> _
2 Likes

Hi Wisecracker,

Thank you, this is worked and I can live with this :slight_smile: .

import os
sys=os.sys
sys.path.append("/path/to/config")
from test_properties import *
params=paramsOriginal
print(params)
1 Like

I don't think you need to use append, to be honest:

sys.path

According, this may work (sorry, did not test it), if ("/path/to/config") is the full path:

sys.path("/path/to/config")

Try it and post back :slight_smile:

Hi Neo,

As requested :slight_smile:

I tried, there is a error:

import os
sys=os.sys
sys.path("/path/to/config")
from test_properties import *
params=paramsOriginal
print(params)

Error:

TypeError: 'list' object is not callable
1 Like

Thanks for posting back your test results.