"ImportError: No module named..." when importing my own module

33,311

Solution 1

OK so I finally worked it out. As indicated by a few of the answers I needed to add my root folder to the system path.

In the end this is what I did:

import sys
sys.path.append("/home/lucasamos/FYP")

Solution 2

you should try this:

import sys
sys.path.append("../Shares/templates")
import share_data

It adds your templates folder to the list of path python is checking for modules.

Solution 3

Add empty __init__.py on one level with manage.py file.

Such inclusion of __init__.py file indicates to the Python interpreter that the directory should be treated as a Python package.

Share:
33,311
Lucas Amos
Author by

Lucas Amos

I'm a Senior Cloud Software Engineer working with AWS, Terraform, Node, Typescript and Snyk. I love building CI/CD pipelines and writing dank code. I hold a Masters Degree in Advanced Computer Science from the University of St Andrews.

Updated on July 27, 2022

Comments

  • Lucas Amos
    Lucas Amos almost 2 years

    I am trying to import a module and I keep getting an ImportError.

    In the PortfolioStatus.py file I have the following code which imports the share_data class from the share_data.py module
    from Shares.share_data import share_data

    I am getting the following error:

    File "/home/lucasamos/FYP/Shares/Communication/PortfolioStatus.py", line 3, in <module>
    from Shares.share_data import share_data
    ImportError: No module named Shares.share_data
    

    To make things more confusing this works fine on my local machine but I am hosting on PythonAnywhere and this is where I am getting the error

    My file hierarchy is show in the image below

    File hierarchy

    Thanks in advance!