How to Import a Python File to Work in a Python GUI
Python is a higher-level programming language that is quickly gaining acceptance among computer programmers. It is easy to learn and use, is portable and cross-platform, and is less syntax-dependent that lower-level languages such as C and C++.
One of Python's strengths is its flexibility and interchangeability. Existing programs can be added to with additional, smaller programs called modules. Each module is a self-contained Python program, and importing one into a Python program is a simple process.
Instructions
-
-
1
Open your Python program in a text editor such as Notepad (Windows,) TextWrangler (Mac) or vi or emacs (Linux). It is important that you open the file in a text editor and not a word processor, as word processors add invisible end-of-line and other characters that interfere with the Python interpreter.
-
2
Add the file you wish to import. At the beginning of your Python program, type "import xxx" (without the quotation marks), where xxx.py is the name of your file. Make sure you do not type the ".py" extension.
-
-
3
Save the program. If the module you just imported is one you wrote yourself, make sure that it is located in the same directory as your new Python program. Official Python modules such as "math" and "os" are already loaded into your Python installation.
-
4
Run the program by typing "python example.py" at the command line. The Python interpreter will automatically load and run the imported file.
-
1
References
- "Learning Python"; Mark Lutz; 2008