Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import double extension files in python

I have a python file called

default.properties.py

How can I successfully import it as a module, I tried

import default.properties as prop

but it didn't work until I changed the name to default.py, Id like to keep the name with two extensions, is this possible?

like image 312
user2798694 Avatar asked Feb 16 '26 00:02

user2798694


1 Answers

You can use imp

import imp
mymodule = imp.load_source('default.properties','default.properties.py')
>>>mymodule.variable
"i am variable in default.properties.py"

OR

mymodule = imp.load_module('default.properties',
                            open('default.properties.py'),
                            os.path.abspath('default.properties.py'),
                            ('.py', 'r', imp.PY_SOURCE))
>>>mymodule.variable
"i am variable in default.properties.py"    
like image 146
itzMEonTV Avatar answered Feb 18 '26 14:02

itzMEonTV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!