user1310420
user1310420

Reputation:

Python import module from relative folder

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,

Upvotes: 0

Views: 107

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121992

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.

Upvotes: 2

Related Questions