27

I know that there are a low of similar issues, but none helped me, so I am writing a new one. Here is my directory structure:

- mypackage
    - __init__.py
- run.py
- requirements.txt

The run.py content:

from mypackage import app

app.run(host='localhost', port=3648)

The mypackage/_init_.py content:

from flask import Flask

app = Flask(__name__)

And here is the full error:

C:\...\parser>python run.py
Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from mypackage import app
ImportError: cannot import name 'app' from 'mypackage' (unknown location)

It seems to be a bug or I am doing something wrong..

UPDATE: Environment check for PYTHONPATH:

Traceback (most recent call last):
  File "run.py", line 6, in <module>
    print(os.environ['PYTHONPATH'])
  File "C:\Users\white\AppData\Local\Programs\Python\Python37\lib\os.py", line 678, in __getitem__
    raise KeyError(key) from None
KeyError: 'PYTHONPATH'
GergelyPolonkai
  • 6,230
  • 6
  • 35
  • 69
Andrii H.
  • 1,682
  • 3
  • 20
  • 40

7 Answers7

12

tl;dr: rename your package

Was your package really named mypackage? I guess not. :)

I had had the same error. In my case, the name I chose for mypackage happened to be the name of an existing Python library I didn't know about. Once I renamed my package, the error disappeared.

jciloa
  • 1,039
  • 1
  • 11
  • 22
  • 5
    Before downvoting this answer: Note that it DOES solve the issue in some cases. If it does not solve it in your case, upvote the one that does, or add your own answer. If you just don't like the way it is worded, please suggest a better wording. Thanks! – jciloa Nov 10 '20 at 16:46
3

I guess when you are running run.py, the current aka working directory is not where that file is. So mypackage is not on sys.path.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
3

i faced the same problem. i renamed a few files from init.py to '__init__.py'

  • you saved me! In my case I didn't have any init files, so just created an empty `__init__.py`, and it fixed the error – Ivan Nov 07 '22 at 19:19
2

To me this happened when installing a local version of a pip package which I wanted to debug. The package was named mypackage the same as the folder of which I cloned it via git. You would think that this is not an issue, as it is the default git behavior. But pip confuses this, if the folder name and the package name is the same. I renamed the local git clone to mypackage_git.

Afterwards I installed via

mv mypackage mypackage_git
cd mypackage_git
pip -e .
NicoHood
  • 687
  • 5
  • 12
1

Happened to me when Pipenv virtual environment was somehow broken. Deleted the virtualenv and let Pipenv make a new one.

And it worked again.

PunchyRascal
  • 280
  • 2
  • 9
0

One answer that I don't see here is that Python doesn't recognize modules if they're not suffixed with .py. In my case, it turned out that the module in question wasn't properly suffixed. If you get this error, check if all of your module files are correctly named.

Alex.L
  • 1
  • 1
0

keep one init.py file inside the folder. Then it will import as a Module else it will give same error as unknown location

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 02 '22 at 02:40