ruipacheco
ruipacheco

Reputation: 16402

PyCharm cannot find the packages in virtualenv

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?

Upvotes: 49

Views: 69576

Answers (14)

ivanleoncz
ivanleoncz

Reputation: 10005

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)

Upvotes: 0

Ggjustice V
Ggjustice V

Reputation: 97

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

Upvotes: 0

Thomas Bayer
Thomas Bayer

Reputation: 71

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.

Upvotes: 0

Tatiana Parshikova
Tatiana Parshikova

Reputation: 1

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.

Upvotes: 0

alphazwest
alphazwest

Reputation: 4450

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.

Upvotes: 5

ggrelet
ggrelet

Reputation: 1091

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.

Upvotes: 0

Joseph Pleuss
Joseph Pleuss

Reputation: 31

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.

Upvotes: 3

Ben Yitzhaki
Ben Yitzhaki

Reputation: 1416

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.

Upvotes: 0

Jagadeesh
Jagadeesh

Reputation: 149

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.

Upvotes: -1

Eric Lou
Eric Lou

Reputation: 169

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

Upvotes: -1

Eyedia Tech
Eyedia Tech

Reputation: 145

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

Upvotes: 3

dieend
dieend

Reputation: 2299

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

Upvotes: 10

Nathaniel Ford
Nathaniel Ford

Reputation: 21220

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.

Upvotes: 88

J.Ku
J.Ku

Reputation: 123

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.

Upvotes: 0

Related Questions