PyCharm Not Properly Recognizing Requirements – Python, Django
Question:
Often requirements show up in requirements.txt
like this:
django-registration
But in INSTALLED_APPS
it appears as registration
. So when you import like:
from registration.signals import user_registered
This is valid but PyCharm is showing a red squiggly line under the word registration in from registration...
that says “Package ‘registration’ is not listed in project requirements”.
How can I remedy this?
UPDATE
I was able to find a way to make the warning go away but it’s not a good solution. If you simply add the package name to the requirements.txt
file the warning goes away, but then that requirement is not valid if you were to install with pip
. For example, I’m also using pygooglevoice
which is how it’s written in requirements.txt
but when it’s imported in the code, you write:
from googlevoice import Voice
This seems weird because I use PyCharm for many projects and I’m just noticing this with a recent project I’m working on…
Answers:
After seeing this phenomenon again, I did some more digging. After setting certain folders as source roots and restarting PyCharm, these reference warnings went away. I think this is a bug in PyCharm.
This seem to be an open issue
PyCharm should be able to figure this out, but it doesn’t. For now, I just add # noinspection PyPackageRequirements to each import with this warning to suppress it. I’d rather not, but it works for now.
JetBrains claims that this is fixed in version 2017.2:
https://youtrack.jetbrains.com/issue/PY-11963#comment=27-2248728
I can confirm this (in IntelliJ IDEA). Also, it’s worth double-checking that the correct requirements file is specified in Settings -> Tools -> Python Integrated Tools -> Package requirements file
This issue can be fixed using the following commands in the terminal (using linux, not sure if this will work on windows, but I see no reason why it shouldn’t, I haven’t tried on windows.)
If you haven’t created a requirements.txt file already
touch requirements.txt
This should do the trick
pip freeze > requirements.txt
pip install flask
For better understanding, I recommend reading this
https://boscacci.medium.com/why-and-how-to-make-a-requirements-txt-f329c685181e
Often requirements show up in requirements.txt
like this:
django-registration
But in INSTALLED_APPS
it appears as registration
. So when you import like:
from registration.signals import user_registered
This is valid but PyCharm is showing a red squiggly line under the word registration in from registration...
that says “Package ‘registration’ is not listed in project requirements”.
How can I remedy this?
UPDATE
I was able to find a way to make the warning go away but it’s not a good solution. If you simply add the package name to the requirements.txt
file the warning goes away, but then that requirement is not valid if you were to install with pip
. For example, I’m also using pygooglevoice
which is how it’s written in requirements.txt
but when it’s imported in the code, you write:
from googlevoice import Voice
This seems weird because I use PyCharm for many projects and I’m just noticing this with a recent project I’m working on…
After seeing this phenomenon again, I did some more digging. After setting certain folders as source roots and restarting PyCharm, these reference warnings went away. I think this is a bug in PyCharm.
This seem to be an open issue
PyCharm should be able to figure this out, but it doesn’t. For now, I just add # noinspection PyPackageRequirements to each import with this warning to suppress it. I’d rather not, but it works for now.
JetBrains claims that this is fixed in version 2017.2:
https://youtrack.jetbrains.com/issue/PY-11963#comment=27-2248728
I can confirm this (in IntelliJ IDEA). Also, it’s worth double-checking that the correct requirements file is specified in Settings -> Tools -> Python Integrated Tools -> Package requirements file
This issue can be fixed using the following commands in the terminal (using linux, not sure if this will work on windows, but I see no reason why it shouldn’t, I haven’t tried on windows.)
If you haven’t created a requirements.txt file already
touch requirements.txt
This should do the trick
pip freeze > requirements.txt
pip install flask
For better understanding, I recommend reading this
https://boscacci.medium.com/why-and-how-to-make-a-requirements-txt-f329c685181e