51

I have the latest PyCharm CE and am using it with virtualenv. I have defined the interpreter as the interpreter in the virtualenv. The Project Interpreter window in PyCharm lists all the packages I have installed. I confirmed this by running pip freeze > requirements.txt and running through the packages manually.

My problem is that PyCharm won't find certain includes in its editor windows, like Flask-Login:

In from flask.ext.login import current_user, login_user, logout_user, login_required the includes current_user, login_user, logout_user, login_required are all marked as unresolved references.

Am I missing something?

4
  • 3
    I've had this issue once and it was just jetbrains being lame. Try File -> Invalidate Caches and restart PyCharm. You might have to wait some minutes for it to rebuild the caches Commented Dec 29, 2015 at 23:04
  • 1
    In my case, I messed up my venv by accidentally adding a new venv where my old one previously was instead of just selecting the old one. The solution was to delete the venv directory out of my Pycharm project (this deleted my local venv as well). After that, I set up a fresh venv and added that one to Pycharm correctly this time. Commented Jan 28, 2020 at 14:14
  • 1
    It's 2021 now and I have still the same problem... (Pycharm 2021.3.1 Professional E.). None of the current answers worked for me. Commented Jan 14, 2022 at 13:49
  • It didn't work for me when I had Non English characters in parent directory (not project dir itself) Commented Sep 10, 2024 at 9:35

14 Answers 14

89

The problem may lay in PyCharm picking up faulty 'Interpreter Paths' for your virtual environment. Go here:

PyCharm (menu) -> Preferences (Menu option) -> Project: <name> (Dropdown) -> Project Interpreter (Menu option) -> 'Settings' button (Looks like a gear) -> More (Menu option) -> Select your virtualenv interpreter -> Click 'Show paths for interpreter' button (on bottom of list window) 

Now that you're in this (admittedly tortuously found) location, you should see paths being used by this interpreter. If my theory is correct, these are pointing to global system locations. To add the virtual environment paths, you should click the + button and add corresponding paths that exist inside your virtual environment. Once you're done with this, it's a good idea to select the global system paths and click - to remove them. Click apply, and go to File -> Invalidate caches / Restart to reload PyCharm.

This should get your interpreter to be pointed to the correct location for the libraries you've installed into your virtualenv, and you should no longer be getting the import error. Note that even with this fix you will not see your libraries under the Project Interpreter, but they should be being loaded.

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

2 Comments

This is the correct procedure in fact (although it is somehow ridiculous that PyCharm is so primitive on these things).
File -> Invalidate caches / Restart to reload PyCharm. what I hadn't done.
11

In the newest version of PyCharm (2016.1.4 in my case):

  • Settings
  • Project: name of project
    • project Interpreter
    • at the right side there will be a dropdown where you can choose the interpreter. There should be venv options.

See image below for better explanation (like they said, 1 picture worth thousands word)

Setting python interpreter

3 Comments

This answer is unclear; what do I do with the venv options? Are you trying to reference some other comment tacitly?
@rrlamichhane the question is explicitly stating that they use it with virtualenv, and venv is the default folder created when using virtualenv. I believe you just need to use the Python interpreter in the virtualenv. If your installed packages are in the virtualenv, it should just work.
it should, but it doesn't.
5

Also note the accepted answer is no longer applicable to PyCharm menu structure. It is now File > Settings > Project > Project Interpreter > Gear Icon > Show All

The following steps detail the "nuclear" option:

  1. Delete your project virtual environment directory (e.g. /venv)
  2. Delete all other interpreters listed in menu option accessible by the route listed at the top of this post.
  3. Close PyCharm
  4. Delete the .idea directory in your project folder
  5. Restart PyCharm, opening the project folder.
  6. Go through the process of configuring a new interpreter.

That will pretty much get you starting from scratch.

Comments

3

I was also facing the same issue (includes are still not being found) even after Nathaniel Ford and dieend's correct suggestion. Make sure that your run/debug configuration as correct python interpreter selected:

enter image description here

Comments

3

I noticed that every time I open a different project it still has the venv from the project I was previously working on.

What I do is:

ctrl-alt-s (to go into preferences), then Project Interpreter/settings (gear icon), show all, then remove all the venv environments that aren't your current project (use the - sign). Restart, and you should be good to go.

Comments

0

Open up Preferences -> Project -> Project Interpreter, do you see the module there?

If yes, you might have another file somewhere in your project have the same name as flask.ext.login, this prevents pycharm from locating the actual module.

If no, you can click on the ... beside your interpreter and select more..., select your interpreter and at the bottom (beside the filter), click the Show paths for the selected interpreter, you can add the path of your module there.

2 Comments

Tried what you suggested but it didn't work for me. I even created a new virtualenv with pypy but the same includes are still not being found.
sorry, I have no idea how to fix your problem.
0

For me, the easiest solution was to open the project in the root directory (my project has a server and client directories, thus the root directory contained both of them). When you open the project in the root directory, it is able to find the dependencies without messing with pycharm settings as it uses them by convention.

Comments

0

My two cents on this topic as I struggled myself with it recently. Nathaniel Ford's answer is the good one except that this part:

 -> Select your virtualenv interpreter 

was unclear to me.

I tried several times with

~/.virtualenvs/python-audition-2.9/bin/python 

whereas it only worked with

~/.virtualenvs/python-audition-2.9/local/bin/python 

Notice the .../local/... in the latter path? It was really important in my case. And don't forget to File -> Invalidate caches / Restart to reload PyCharm.

Comments

0

Adding the lib directory in my virtual environment to sources in the PyCharm CE settings helped me. My steps:

  1. Preferences -> Project: -> Project Structure

Scrin1

  1. Select the directory where you installed the libraries. In my case, this is "lib".

  2. Mark the directory as "source".

Scrin2

After these actions, all my libraries were correctly imported into the py files I needed.

Comments

0

Easy solution: (PyCharm 2022.2.3) and Python 3.11.0 on Win11:

  • Create new venv Environment for the project, DO NOT inherit site-packages
  • Check, if jupyter-server uses new venv (Settings->Language&Frameworks->Jupyter->Jupyter Server)

Install all required packages (inbcluding jupyter!) to venv (e.g., requirements.txt and PyCharm) Worked fine for me.

Comments

0

I had same issue on WINDOWS and LINUX and i continue to have same issue each time i move project to different location... What works for me is to

rename venv directory

inside your project to venvxx(or w/e you want) and then

create NEW virtual env

through pycharm settings After that you

paste lib directory

from venvxx(or w/e named it)

to lib inside fresh new venv

and it should work now at least for me it does i hope this helps someone out there! After that restart pycharm and your terminal should be working fine

Comments

0

Here's corner case: if you moved your project folder to some other place in your file-system, then some caching of PyCharm on your project folder, might be forcing and old interpreter path behind the scenes, even though you defined a new one. And of course, you'll see no packages from your Virtualenv, for the interpreter object itself, was not recognized by PyCharm.

If that's the case, there are two options:

  1. erase the folder .idea from your project's root folder, close PyCharm and open it up again, in order to set up your Python interpreter.
  2. invalidate PyCharm cache: File -> Invalidate Caches...

For debugging PyCharm logs: Help -> Show Log in Files...

On Ubuntu, generally, the main PyCharm log is available on the following path:

  • ~/.cache/JetBrains/PyCharmCE2023.1/log/idea.log: the directory PyCharmCE2023.1 is named based on your PyCharm installation date, so it might be a different directory on your file-system.

You can use PyCharm while visualizing the log file and the possible errors related with the interpreter setup, in order to figure out the issue:

$ tail -f .cache/JetBrains/PyCharmCE2023.1/log/idea.log 2023-06-01 20:37:35,376 [22188086] INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files 2023-06-01 20:41:17,038 [22409748] INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files 2023-06-01 20:42:02,206 [22454916] INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files 2023-06-01 20:42:23,752 [22476462] INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files 2023-06-01 20:42:32,623 [22485333] INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files 2023-06-01 20:49:01,659 [22874369] INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files 2023-06-01 20:49:09,130 [22881840] INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files 2023-06-01 20:49:11,019 [22883729] INFO - #c.i.i.a.RevealFileAction - Timed out 2023-06-01 20:49:11,019 [22883729] INFO - #c.i.i.a.RevealFileAction - Nautilus-Share-Message: 20:49:01.414: Called "net usershare info" but it failed: Failed to execute child process “net” (No such file or directory) 

Comments

-1

Goto /venv/bin/ and check all activate scripts. You venv path might be wrong.

Comments

-1

I was not able to assign existing virtual environment to my project, but after going to

File -> Settings -> project interpreter-> show all-> click on '+'

to create a new virtual environment or we can choose the existing virtual environment, I am able to assign and use the existing virtual enviroments.

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.