0

i'm creating a python program, and i want to split it up into seperate files. I'm using import to do this, but its not working (specifically, a variable is stored in one python file, and its not being read by the main one.

program /
 main.py
 lib /
 __init__.py
 config.py
 functions.py

I have in main.py:

import lib.config
import lib.functions
print(main)

and config.py has

 main = "hello"

I should be getting the output "hello", when i'm executing the file main.py, but i'm not. I have the same problem with functions stored in functions.py

Any help would be great,

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Mar 2, 2013 at 9:37
3
  • print(lib.config.main)? Commented Mar 2, 2013 at 9:40
  • Have you imported these two files in __init__.py? Commented Mar 2, 2013 at 9:41
  • @solusipse: that is not required at all. Commented Mar 2, 2013 at 9:43

1 Answer 1

2

Importing the module with a simple import statement does not copy the names from that module into your own global namespace.

Either refer to the main name through attribute access:

print(lib.config.main)

or use the from ... import ... syntax:

from lib.config import main

instead.

You can learn more about how importing works in the Modules section of the Python tutorial.

answered Mar 2, 2013 at 9:41
Sign up to request clarification or add additional context in comments.

1 Comment

Got it. I just user from lib.config import * so that i got everything

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.