1

I'm working in a Python 3 project using Pycharm and I have a problem importing a file. This is my project structure:

twixer |----- docs |----- twixer |----- __init__.py |----- config.ini |----- facepp.py |----- twixer.py |----- setup.py 

In twixer.py I have the next line:

import twixer.facepp 

But that line throws this error when I run the project:

Traceback (most recent call last): File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked AttributeError: 'module' object has no attribute '__path__' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:/Users/David/PycharmProjects/twixer-py/twixer/twixer.py", line 2, in <module> import twixer.facepp File "D:\Users\David\PycharmProjects\twixer-py\twixer\twixer.py", line 2, in <module> import twixer.facepp ImportError: No module named 'twixer.facepp'; 'twixer' is not a package 

I have no idea about how to solve this problem. I tried to change the way I import the file without luck. What is the problem? How can I fix it?

5
  • 1
    wouldnt import faceapp do the job ? Commented Apr 26, 2015 at 20:15
  • That's the first thing I tried but Pycharm shows this error: No module named facepp. Commented Apr 26, 2015 at 20:17
  • @ThePavolC not sure about what happens but I renamed root folder to 'twixer-py' and back to 'twixer' and now import facepp works, but Pycharm still shows the same error on the editor. Commented Apr 26, 2015 at 20:31
  • How do you run the project? Did you used your setup.py to install it? Commented Apr 26, 2015 at 20:38
  • @pacholik Right click over twixer.py and Run. Thats all. Commented Apr 26, 2015 at 20:39

3 Answers 3

1

I made the same beginner mistake of executing the code in the console instead of running it (right click in file and select "Run '_your_python_file_'").

Hope this can help others beginners.

Sign up to request clarification or add additional context in comments.

Comments

0

This is too long if answer it in coments, so post it here, I dont know if it works for you or not, just provide some idea. I had similar issue, and I solved it by adding the module in python path

hello-world |----- helpers |----- __init__.py |----- helper.py |----- sdp_helper.py |----- say-hello |----- __init__.py |----- say_hello.py 

When I on use the scripts on console:

sys.path.insert(0, "/home/haifzhan/hello-world/helpers") import helper import sdp_helper 

When I use the module in Pycharm, the above imports are not working, so I use:

from hello-world.helpers import helper from hello-world.helpers import sdp_helper 

1 Comment

But in question, faceapp.py and twixer.py are in the same folder. You should be able to import .py files from same folder
0

base on the traceback of your error, I think your problem may be that your module and package have the same name, thus when you try to import twixer.facepp, Python uses its Zen "In the face of ambiguity refuse the temptation to guess".

Python avoids guessing if you meant import the module twixer or import the package twixer, thus its raise an error for you to correct it.

You could also solve this without changing your module/package name (although I would recommend that), by using a relative import, i.e

from . import facepp 

I hope it helps ;)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.