ImportError when trying to import a script module?

I write a code to import a particular module I create called “someModule()”, and if I do it like, from exampleScript import someModule, even without the brackets, this happens:

Python module can’t be imported - object ‘Cube’, controller ‘Python’:
Traceback (most recent call last):
File “exampleScript” line 4 in
ImportError: No module named ‘exampleScript.py’; ‘exampleScript’ is not a package

Seems weird… the BGE thinks its a “package”, when its clearly part of a script. A module is part of a script, not a package, which I believe is different.

This is an example scenario a typical person may run into.

You can even make your own modules in Python. Just use def example(): and some text to import the bge and logic and then use the example() to create the module! You can even do it with def main(): at the top of the script! (new module must be indented)

from textFileName import definition

If the py file is on the same folder your blendfile is at, it should import.
It’d be useful if we could see the few lines of code that console is freaking out about.

I meant from a relative directory on the hard drive (e.g //path/folder/file.blend) or /path/folder/file.blend

Append the directory to sys.path

When I import a module with a (), it will result in a syntax error. How can I get around this?

Learn proper syntax.

to import func -> from module import function
to call func-> function()

so remove parenthesis.

remove the .py from the script (not from the script name)

so you have examplescript.py
with a module something()

then in the script you want to import it do:
from examplescript import something

or simply import the whole script.
import examplescript as script

now examplescript will be called: script.
then do script.something to call the function

Modules/Packages are similar in Python, but a module would be a Python file, named like some_name.py and that you import like import some_name. But it has to finish with a .py extension.

Then inside modules/packages you can have anything, such as functions, that you might import doing:

from some_name import some_function

some_function()

Or

import some_name

some_name.some_function()