PyCharm cannot find the packages in virtualenv

41,558

Solution 1

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.

Solution 2

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

Solution 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

Solution 4

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.

Solution 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.

Share:
41,558
ruipacheco
Author by

ruipacheco

Updated on July 27, 2021

Comments

  • ruipacheco
    ruipacheco almost 3 years

    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?

  • ruipacheco
    ruipacheco over 8 years
    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.
  • J.Ku
    J.Ku over 8 years
    sorry, I have no idea how to fix your problem.
  • gented
    gented over 6 years
    This is the correct procedure in fact (although it is somehow ridiculous that PyCharm is so primitive on these things).
  • Yuseferi
    Yuseferi about 6 years
    File -> Invalidate caches / Restart to reload PyCharm. what I hadn't done.
  • rrlamichhane
    rrlamichhane almost 4 years
    This answer is unclear; what do I do with the venv options? Are you trying to reference some other comment tacitly?
  • dieend
    dieend almost 4 years
    @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.