Reputation: 1812
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'
Upvotes: 32
Views: 72764
Reputation: 131
This happened to me when I have two feature branches (old, new), I was working on the new branch where old branch is pending merge request from my colleague. After he gave me some feedback I checkout to old branch to fix something first, then went back to the new branch to continue developing. I have forgot the new branch has a conftest.py
file but the old branch not.
In my old branch, I did not use conftest.py
, and I mock the tests in the same script that uses it. Unit tests run well.
After that, I merge the old branch into the master branch, then checkout to my new branch, merge master branch into that, then pytest failed. This is because the new branch has conftest.py
file that mocks away some modules, and that overrides whatever I was mocking inside the old branch's tests.
To solve this, I rethink my unit tests then remove my conftest.py
, change my ways of mocking, then run the tests again, works as intended.
Upvotes: 0
Reputation: 1123
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 .
Upvotes: 5
Reputation: 11
keep one init.py file inside the folder. Then it will import as a Module else it will give same error as unknown location
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1137
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.
Upvotes: 16
Reputation: 360
Happened to me when Pipenv virtual environment was somehow broken. Deleted the virtualenv and let Pipenv make a new one.
And it worked again.
Upvotes: 3
Reputation: 71
i faced the same problem.
i renamed a few files from init.py to '__init__.py
'
Upvotes: 4
Reputation: 36086
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
.
Upvotes: 3