Tux
Tux

Reputation: 247

Python 3 Include Custom File with Variables

How to include external config file with variables and use them in python script ?

Python Variables File: (auth.txt)

Username = 'username_here'
Password = 'password_here'

Python Script (test.py)

print('Username:', Username)
print('Password:', Password)

Upvotes: 0

Views: 726

Answers (2)

drz
drz

Reputation: 992

configparser will let you treat it like a dictionary:

config.txt

[DEFAULT]
USR = user_here
PWD = pass_here

auth.py:

import configparser
config = configparser.ConfigParser()
config.read('./config.txt')
print(config['DEFAULT']['USR'])
print(config['DEFAULT']['PWD'])

yields:

user_here
pass_here

Upvotes: 2

Tux
Tux

Reputation: 247

I got it by using;

auth.py

# Python Variables
USR = 'user_here'
PWD = 'pass_here'

test.py

#!/bin/python -B
from auth import *
print('Username:', USR)
print('Password:', PWD)

Upvotes: 0

Related Questions