Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to "source" file into python script

I have a text file /etc/default/foo which contains one line:

FOO="/path/to/foo"

In my python script, I need to reference the variable FOO.

What is the simplest way to "source" the file /etc/default/foo into my python script, same as I would do in bash?

. /etc/default/foo
like image 364
Martin Vegter Avatar asked Apr 30 '16 19:04

Martin Vegter


People also ask

How do I import a file into python?

You need to tell python to first import that module in your code so that you can use it. If you have your own python files you want to import, you can use the import statement as follows: >>> import my_file # assuming you have the file, my_file.py in the current directory.


5 Answers

Same answer as @jil however, that answer is specific to some historical version of Python.

In modern Python (3.x):

exec(open('filename').read()) 

replaces execfile('filename') from 2.x

like image 84
Hack-R Avatar answered Sep 28 '22 04:09

Hack-R


You could use execfile:

execfile("/etc/default/foo") 

But please be aware that this will evaluate the contents of the file as is into your program source. It is potential security hazard unless you can fully trust the source.

It also means that the file needs to be valid python syntax (your given example file is).

like image 29
jil Avatar answered Sep 28 '22 04:09

jil


If you know for certain that it only contains VAR="QUOTED STRING" style variables, like this:

FOO="some value"

Then you can just do this:

>>> with open('foo.sysconfig') as fd:
...   exec(fd.read())

Which gets you:

>>> FOO
'some value'

(This is effectively the same thing as the execfile() solution suggested in the other answer.)

This method has substantial security implications; if instead of FOO="some value" your file contained:

os.system("rm -rf /")

Then you would be In Trouble.

Alternatively, you can do this:

>>> with open('foo.sysconfig') as fd:
...   settings = {var: shlex.split(value) for var, value in [line.split('=', 1) for line in fd]}

Which gets you a dictionary settings that has:

>>> settings
{'FOO': ['some value']}

That settings = {...} line is using a dictionary comprehension. You could accomplish the same thing in a few more lines with a for loop and so forth.

And of course if the file contains shell-style variable expansion like ${somevar:-value_if_not_set} then this isn't going to work (unless you write your very own shell style variable parser).

like image 25
larsks Avatar answered Sep 28 '22 03:09

larsks


Keep in mind that if you have a "text" file with this content that has a .py as the file extension, you can always do:

import mytextfile

print(mytestfile.FOO)

Of course, this assumes that the text file is syntactically correct as far as Python is concerned. On a project I worked on we did something similar to this. Turned some text files into Python files. Wacky but maybe worth consideration.

like image 25
Harlin Avatar answered Sep 28 '22 04:09

Harlin


Just to give a different approach, note that if your original file is setup as

export FOO=/path/to/foo

You can do source /etc/default/foo; python myprogram.py (or . /etc/default/foo; python myprogram.py) and within myprogram.py all the values that were exported in the sourced' file are visible in os.environ, e.g

import os
os.environ["FOO"] 
like image 44
Foon Avatar answered Sep 28 '22 04:09

Foon