4

I'm receiving an ImportError when running my main script which is related to another script trying to import one of my modules and I'm unsure how to fix it. The layout of the files in relation to the software is as follows (folder names etc are fictional):

poetry_generator/main.py poetry_generator/architecture/experts/poetryexperts/sentence.py poetry_generator/structures/word.py 

Main.py is what I'm running, and the problem seems to arise from sentence.py trying to import word module and failing.

Currently in sentence.py I have:

from poetry_generator.structures.word import Word 

Word is the name of the Class in word.py: Class Word(object). But I'm receiving the following error: ImportError: No module named poetry_generator.structures.word

Does anyone have any idea what is wrong? I have been readinga roudn similar questions already asked but nothing has worked so far. Thanks in advance for any help.

Full console text reagarding error:

Traceback (most recent call last): File "main.py", line 1, in <module> from architecture.control_component import ControlComponent File "/home/lee/Downloads/PoEmo-master/poetry_generator/architecture/control_component.py", line 4, in <module> from experts.poem_making_experts import exclamation_expert File "/home/lee/Downloads/PoEmo-master/poetry_generator/architecture/experts/poem_making_experts/exclamation_expert.py", line 5, in <module> from poetry_generator.structures.word import Word ImportError: No module named poetry_generator.structures.word 
9
  • 2
    Do you have a file called __init__.py in both System/ and structures/? Commented Apr 24, 2017 at 11:07
  • I do, I have these in each of the folders. One with text in. Could this be the problem? Commented Apr 24, 2017 at 11:19
  • No, that's a good thing. Are you directly running sentence.py, e.g. via python sentence.py, or is there another script that you're running as your main program? From what directory are you running your main script? Commented Apr 24, 2017 at 11:24
  • My main script is within poetry_generator (which has an init), the word file is within a subdirectory of this folder, and the file I'm importing word into is located within the poetry_Generator folder, then another folder, and then another. So I'm running mani.py in poetry_generator, two subdirectory in is the sentence.py which imports word.py from a different subdirectory of poeytr_generator - if that makes sense. All have inits Commented Apr 24, 2017 at 11:30
  • That is difficult to follow in a comment, and it's likely related to your problem. Please edit your question and show the directory structure you just described, identifying the main file. Commented Apr 24, 2017 at 11:33

2 Answers 2

4

The top-level project directory shouldn't be included in the module name. This should work:

from structures.word import Word 
Sign up to request clarification or add additional context in comments.

Comments

2

You will need

import sys sys.path.insert(0, 'System/structures/word') #or sys.path.append('System/structures/word') import Word 

Otherwise you will need __init__.py that you can make with these instructions.

11 Comments

I'll try this out now, system isn't the actual machine, it was just the name I Was using for the software. I do have initis in each of the folders, but for some reason its just not going.
I figured about System, just wanted to follow your train of thoughts with answer :)
I've just tried this, if I change "from poetry_generator.structures.word import Word", to "import sys sys.path.insert(0, 'poetry_generator/structures/word') import Word" I just get an error with no module named Word?
@zipa: word is a file, not a folder, though.
@SiHa Thanks for comment. insert and append should go up to folder.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.