2

I'm not sure why I can't import the db_password variable.

Running db_setup gives:

Traceback (most recent call last):
  File "db_setup.py", line 5, in <module>
    passwd=dbconfig.db_password)
TypeError: 'module' object is not callable

I'm missing something but i'm not sure what.

I can open up an interperter and import dbconfig as well as call `dbconfig.db_password'

db_setup.py

import pymysql
import dbconfig

connection = pymysql.connections(host='localhost', user=dbconfig.db_user, passwd=dbconfig.db_password)

dbconfig.py

db_user='username'
db_password='password'

I expect the module to pass the variable 'password'

  • if you want a config file with username and password I think the best way is to use configparser library ;) https://docs.python.org/3/library/configparser.html – Carlo 1585 Jul 31 '19 at 15:36
  • 1
    The point that python tries to make is that pymysql.connections is a submodule of pymysql and not a function. – Menno Hölscher Jul 31 '19 at 15:38
  • @Carlo1585 https://stackoverflow.com/a/4208528/4289380 i still have no idea if configparser is better than a simple module – Corentin Limier Jul 31 '19 at 15:48

1 Answers1

1

There is no problem with the import of db_password variable, the error is elsewhere :

import pymysql
import dbconfig

connection = pymysql.connect(host='localhost', user=dbconfig.db_user, password=dbconfig.db_password)

pymysql.connections is a module, not a function, and contains the Connection class that is instanciate and returned by pymysql.connect()

Connection class doc :

The proper way to get an instance of this class is to call connect().

Corentin Limier
  • 4,946
  • 1
  • 13
  • 24