Trouble importing a Python module
Question: I'm new to Python and having trouble importing from a different directory.

The file I'm currently in has the path home/pi/example.py

The module I'm trying to import into this file has the path
home/pi/ReactiveEngine/src/PiEngine.py

I've tried the following:

    
    
    import sys
    sys.path.append('home/pi/ReactiveEngine/src')
    import sys.PiEngine.py
    

But it's saying there's no module named PiEngine.py

Can someone let me know what I'm doing wrong?


Answer: When you import, don't put the suffix `.py`. Just do the following:

    
    
    import sys
    sys.path.append('home/pi/ReactiveEngine/src')
    import sys.PiEngine
    

And it should work without the `.py`, because you can only import a `.py`
file, so the suffix is redundant!

Or as Louis said below, you can use the following:

    
    
    import os
    os.chdir('home/pi/ReactiveEngine/src')
    import PiEngine
    

