Skip to content Skip to sidebar Skip to footer

How Do I Import My User-defined Function Without Having To Call The .py File It Is In?

To solve the problem of the (Python) IDLE not having a built-in 'clear' function, I created a user-defined function [cls()], that I have stored in a functions.py file in my sys.pat

Solution 1:

From the beginning of section 3 of the IDLE doc, available both online and as Help => IDLE Help. "Upon startup with the -s option, IDLE will execute the file referenced by the environment variables IDLESTARTUP or PYTHONSTARTUP. IDLE first checks for IDLESTARTUP; if IDLESTARTUP is present the file referenced is run. If IDLESTARTUP is not present, IDLE checks for PYTHONSTARTUP. Files referenced by these environment variables are convenient places to store functions that are used frequently from the IDLE shell, or for executing import statements to import common modules."

In addition, -r file will run the file.

Currently, the startup files are not rerun when one restarts the Shell.

Solution 2:

You should be able to import cls this way:

from functions import cls

Then you can call the method this way:

cls()

Post a Comment for "How Do I Import My User-defined Function Without Having To Call The .py File It Is In?"