PyCharm cannot find the packages in virtualenv
Question:
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?
Answers:
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.
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.
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)
Goto /venv/bin/ and check all activate scripts. You venv path might be wrong.
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.
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.
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:
- Delete your project virtual environment directory (e.g.
/venv
)
- Delete all other interpreters listed in menu option accessible by the route listed at the top of this post.
- Close PyCharm
- Delete the
.idea
directory in your project folder
- Restart PyCharm, opening the project folder.
- Go through the process of configuring a new interpreter.
That will pretty much get you starting from scratch.
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.
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.
Adding the lib directory in my virtual environment to sources in the PyCharm CE settings helped me.
My steps:
- Preferences -> Project: -> Project Structure
-
Select the directory where you installed the libraries. In my case, this is "lib".
-
Mark the directory as "source".
After these actions, all my libraries were correctly imported into the py files I needed.
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.
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?
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.
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.
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)
Goto /venv/bin/ and check all activate scripts. You venv path might be wrong.
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.
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.
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:
- Delete your project virtual environment directory (e.g.
/venv
) - Delete all other interpreters listed in menu option accessible by the route listed at the top of this post.
- Close PyCharm
- Delete the
.idea
directory in your project folder - Restart PyCharm, opening the project folder.
- Go through the process of configuring a new interpreter.
That will pretty much get you starting from scratch.
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.
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.
Adding the lib directory in my virtual environment to sources in the PyCharm CE settings helped me.
My steps:
- Preferences -> Project: -> Project Structure
-
Select the directory where you installed the libraries. In my case, this is "lib".
-
Mark the directory as "source".
After these actions, all my libraries were correctly imported into the py files I needed.
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.