-2

I have a file with name "x.py". This contains a Class "Y".
I have a second file with name "z.py".
x.py and z.py are in the same directory. This directoty has an empty file with name __init__.py

In z.py I do:

from x import Y

This brings ImportError: cannot import name Y

If I do

import x

then I have no exception but aswell nothing from x.py

What could be the problem here?

Update: In server environment, it works. But not in local, which is Windows with sublime text. When entire code of x is in z, then it works aswell locally.

Structure is

-Folder A
---x.py with class Y inside
---z.py with (from x import Y)

Gamsner
  • 117
  • 1
  • 9
  • 2
    What does the file structure look like? – Edeki Okoh Dec 23 '19 at 16:36
  • 1
    https://stackoverflow.com/questions/12172791/changes-in-import-statement-python3 – Pratik Dec 23 '19 at 16:39
  • 4
    After `import x`, does `x.Y` work? It sounds like `Y` simply doesn't exist in `x`, and `import x` works because you make no mention of `Y`. – chepner Dec 23 '19 at 16:40
  • 1
    Does this answer your question? https://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x/59432370#59432370 – Nicolas Gervais Dec 23 '19 at 16:43
  • @EdekiOkoh it a Folder A. Therein a file x.py and a file y.py and an empty file init.py – Gamsner Dec 23 '19 at 23:51
  • 1
    [Please post the current directory in your question similar to David C. Bishop answer here](https://stackoverflow.com/questions/193161/what-is-the-best-project-structure-for-a-python-application). It may be an issue of relative vs absolute imports. Also can you confirm the class is in Folder A. – Edeki Okoh Dec 23 '19 at 23:55
  • @EdekiOkoh updated initial post – Gamsner Dec 24 '19 at 00:07

1 Answers1

0

It is resolved. The solution was:

Somewhere in my pythonpath and my already installed packages, a module had the same name as that one I wanted to import. As the interpreter first had a look into the other module, it did not find the Class I wanted to import.

The solution was simply rename the module to import and aswell the import call itself. A lot of searching for a small issue.

Gamsner
  • 117
  • 1
  • 9