ehsan shirzadi
ehsan shirzadi

Reputation: 4859

How to import a .py file in all files?

Is it possible to import a test.py file in whole project instead of importing it in each file? for example calling it some where in start.py

Upvotes: 0

Views: 66

Answers (2)

Joran Beasley
Joran Beasley

Reputation: 114038

even if it is ... (which in programming is almost always the case) a better question is why would you want to.

It would be difficult to do, and it adds nothing... in fact it can severely hinder development, and you may experience very strange bugs that become very hard to track down


[edit based on OP comment]

your use case sounds like you just want it to be in builtins

start.py

import __builtin__
import my_weird_test_class
__builtin__.tester = my_weird_test_class
from main_entry import main
main()

now in any file you can use

tester.do_something()

without importing tester or whatever

but as I said this tends to be a very bad idea... it is much better to explicitly import it in all your files ...

Upvotes: 1

chepner
chepner

Reputation: 532003

No.

However, suppose you have three modules, a.py, b.py, and c.py. Further, assume that c.py imports b, and b.py imports a. Then in c.py, you can refer to items in a using

b.a.foo

since b imported a into its namespace.

Upvotes: 2

Related Questions