How to import a function from another module...

Steven D'Aprano steve at pearwood.info
Mon Aug 3 21:24:23 EDT 2015


On Tue, 4 Aug 2015 09:57 am, Dwight GoldWinde wrote:

> I am trying to import a function defined in another module.

You can't use spaces in the name of importable Python modules: change the
name from "Simulate typing.py" to "simulate_python.py". You can use spaces
in file names if they are only used as a runnable script and not imported.

Then use this:

from simulate_python import humprint

There's no need to use importlib.

You may need to arrange for the simulate_python file to be placed somewhere
in the Python search path. Do you need help with that?

What you are trying to do with importlib is fight the language. Your life
will be much simpler if you work within the parameters of how the language
is designed to work.



-- 
Steven




More information about the Python-list mailing list