1

I'm trying to use a python file as a config file. In my real program, I let the user specify the config file at the command line.

Here's my example:

some_randomly_named_file.py:

import os
from datetime import timedelta

PROJECT = "my project"
ENABLED_FORMATS = ['xml', 'json', 'yaml']
EXPIRATION=3600

#DEBUG = True
#TESTING = False

LOG_FOLDER = os.path.join(os.path.expanduser('~'), 'logs')

The file is stored here: /foo/bar/baz/some_randomly_named_file.py

...and then in myapp.py:

# parse the command line and get the path
path_to_file = '/foo/bar/baz/some_randomly_named_file.py'

# load the python file at path_to_file into local variable myconfig
# [What do I write here to accomplish this?]

# And now we have a local variable called myconfig
print(myconfig.PROJECT)

Output:

my project
101010
  • 14,866
  • 30
  • 95
  • 172

3 Answers3

2

Like @sandeep mentioned. Just add your source file (config.py) to your sys path. When you add this config.py file to your path, Python treats the file (and its contents) like an ordinary module. Here's a bit more of an explanation:

# Load your config.py as a module
import sys
sys.path.append("/foo/bar/baz/") # Path to config.py parent folder

import config # Import as a regular module

print(config.PROJECT)
"my project"

You can use this very similar question for reference.

Community
  • 1
  • 1
semore_1267
  • 1,327
  • 2
  • 14
  • 29
  • The file name and path are randomly named. I won't know the path, nor the name of the file at compile time. I couldn't say "import config" in the code because I don't know the name. I updated the question to reflect that the file is user-defined / randomly named. – 101010 Dec 15 '16 at 12:07
  • Well your question was poorly asked @010110110101. Please make sure to include all of this relevant information in the question, and update the question as needed so that we don't submit virtually irrelevant answers. – semore_1267 Dec 15 '16 at 17:31
1

Try adding your path to sys.path

Ex: if your config file is myconfig.py and present under /foo/bar/baz.

import sys
sys.path.append("/foo/bar/baz/")
import myconfig
local_var = myconfig.PROJECT

You can use os.path.basename() and os.path.dirname() to retrieve the values entered by user.

Sample program to get config filename and directory path from User

test.py

import sys
import os

file_path = sys.argv[1]
dir_name = os.path.dirname(file_path)
file_name = os.path.basename(file_path)

sys.path.append(dir_name)
print dir_name
print file_name

Output

#python test.py /var/log/syslog
/var/log
syslog
sandeep nagendra
  • 422
  • 5
  • 15
  • That won't work for this. How would the variable "myconfig" (in my example) become populated like that? – 101010 Dec 15 '16 at 04:55
  • Do you want to import all the contents of a module into a variable? I do not think that is possible. – sandeep nagendra Dec 15 '16 at 05:07
  • sys.path.append("/foo/bar/baz/") import myconfig #assign to local variable my_local_var = myconfig.PROJECT – sandeep nagendra Dec 15 '16 at 05:08
  • my understanding of your question is that you want to access the content of your module via arbitrary path. Please correct if i am wrong. – sandeep nagendra Dec 15 '16 at 05:09
  • The file name and path are randomly named. I won't know the path, nor the name of the file at compile time. I couldn't say "myconfig" in the code. I updated the question to reflect that the file is user-defined / randomly named – 101010 Dec 15 '16 at 12:07
  • I have added example. using that you can handle random filename and directory path. – sandeep nagendra Dec 16 '16 at 06:15
0

I found the solution based on an answer from @Redlegjed in this SO article.

Here it is modified a bit:

import os
import importlib.machinery

path_to_file = '/foo/bar/baz/some_randomly_named_file.py'

module_dir, module_file = os.path.split(path_to_file)
module_name, module_ext = os.path.splitext(module_file)

x = importlib.machinery.SourceFileLoader(module_name, path_to_file).load_module()

print(myconfig.PROJECT)

A list of related and helpful articles:

How to import a module given the full path?

Python 3.4: How to import a module given the full path?

Import arbitrary python source file. (Python 3.3+)

Community
  • 1
  • 1
101010
  • 14,866
  • 30
  • 95
  • 172