11

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!

Lucas Amos
  • 1,117
  • 4
  • 15
  • 36
  • What's the working directory in your run configuration? That will get added to `sys.path`. If you make it the project directory (by leaving it blank) `Shares` should be importable. – Peter Wood Feb 22 '16 at 14:35
  • Check out the [detailed guide to debugging sys.path and import issues on PythonAnywhere](https://help.pythonanywhere.com/pages/DebuggingImportError) – hwjp Feb 24 '16 at 12:32

4 Answers4

11

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.

CoMartel
  • 3,521
  • 4
  • 25
  • 48
  • The `share_data.py` file isn't in the templates folder, it's in the `Shares` folder. I just added `Shares` to the path however and that didn't work :( – Lucas Amos Feb 22 '16 at 14:28
  • 1
    My bad, I miss the closed folder. Try adding the complete path instead of the relative path , e.g "/home/YourName/PycharmProjects/FYP/Shares" – CoMartel Feb 22 '16 at 14:34
  • You need to add the containing directory, not the package itself. – Peter Wood Feb 22 '16 at 14:37
  • @PeterWood could you elaborate? I'm afraid I'm a bit of a python n00b – Lucas Amos Feb 22 '16 at 14:42
11

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")
Lucas Amos
  • 1,117
  • 4
  • 15
  • 36
  • 1
    hooray! for anyone else with similar problems, there's a [detailed guide to sys.path and import errors on pythonanywhere](http://help.pythonanywhere.com/pages/DebuggingImportError) in the docs – hwjp Feb 26 '16 at 07:19
  • adding the ROOT folder solved it for me! Important that its the FULL path!! thanks lucas – Brett Young Oct 29 '22 at 13:41
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.

Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
0

This is likely because your Shares directory is not in your PYTHONPATH.

See this article about using PYTHONPATH: https://users-cs.au.dk/chili/PBI/pythonpath.html

Excerpt:

Often however, you will need to import a module not located in the same directory as the main program. Continuing the example above, assume you're writing a program located in ~/PBI/ which needs to include mymodule.py.

In order for the Python interpreter to find your module, you need to tell it where to look. You can do that by setting the environment variable PYTHONPATH. Depending on the shell program you use (e.g., xterm), this is done in one of two ways.

Bash:

export PYTHONPATH=${PYTHONPATH}:/users/[your username]/PBI/Modules/

Community
  • 1
  • 1
Jeff
  • 1,122
  • 8
  • 7
  • 1
    When adding external links (which might at some point in the future become broken) it is customary to also edit the relevant part into your answer. – nestedloop Feb 22 '16 at 14:21