Python (Scripting) - Calling classes from another Python script

by
Jeremy Canfield |
Updated: January 12 2022
| Python (Scripting) articles
Often, shared classes are created in a "master" Python script, so that "child" Python scripts can use the shared classes.
Let's say classes.py contains the following.
#!/usr/bin/python
class Hello:
bar = "World"
Here is how you could call classes.py from another Python script, such as testing.py. In this example, every class in classes.py will be made available in the testing.py script.
AVOID TROUBLE
The "master" Python script being imported will need to reside in one of the sys.path directories.
#!/usr/bin/python
from classes import *
foo = Hello()
print(foo.bar)
Or, if you want to limit the scope of the classes being imported, you can import specific functions.
#!/usr/bin/python
from classes import Hello
foo = Hello()
print(foo.bar)
Did you find this article helpful?
If so, consider buying me a coffee over at