0

Strike this - turns out it was something completely unrelated. (also, ".py" suffixes)

my file structure is as so:

src /
  | main.py
  | filehandler.py

my main.py is failing at the line:

import filehander.py

with the error:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    import filehander
ImportError: No module named 'filehander'

Why is python failing to do the simple task of importing a file in the same directory so badly?

Illiander
  • 43
  • 4
  • You don't import files, you import modules. The file system is just an implementation detail. [You think that's air you're breathing, now](https://www.youtube.com/watch?v=CWaxF8jlnm0)? – Peter Wood Oct 09 '17 at 09:51
  • `import filehander.py` is wrong. you dont write .py in import. `import filehander` is right syntax. – surya singh Oct 09 '17 at 12:45
  • Possible duplicate of [How to import the class within the same directory or sub directory?](https://stackoverflow.com/questions/4142151/how-to-import-the-class-within-the-same-directory-or-sub-directory) – dank8 Apr 01 '18 at 00:56

4 Answers4

0

You don't have to write extension of a file while importing. Just use import filehandler

JJAACCEeEKK
  • 194
  • 1
  • 11
0

Two things:

  1. import filehandler not import filehandler.py
  2. You must be in the /src folder when you run the program. The path is always WRT the current folder.
ssm
  • 5,277
  • 1
  • 24
  • 42
  • I am. When I run "ls" I see both files. – Illiander Oct 09 '17 at 09:57
  • In that case you should not have a problem ... Are you running your python program using `python main.py`? Or are you running it using Spyder or something similar? – ssm Oct 09 '17 at 10:07
0

Check the working directory of your main.py. If you are using an IDE and you moved the file from a directory to an another then the 'configuration' of the file may remained the same and it tries to run where it initially was.

And like others mentioned, use import filehandler, not 'filehandler.py'

Trapli
  • 1,517
  • 2
  • 13
  • 19
0

This error will also occur when there is a syntax error in the imported class. call the class directly to check no errors are returned by pyton:

python.exe filehander.py
dank8
  • 361
  • 4
  • 20