ModuleNotFoundError: No module named 'pyshark' [duplicate] - python

In PyCharm, I've added the Python environment /usr/bin/python. However,
from gnuradio import gr
fails as an undefined reference. However, it works fine in the Python interpreter from the command line.
GNURadio works fine with python outside of Pycharm. Everything is installed and configured how I want it.
Gnuradio is located at /usr/local/lib/python2.7/site-packages/gnuradio
Also:
PYTHONPATH=/usr/local/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages/gnuradio

Adding a Path
Go into File → Settings → Project Settings → Project Interpreter.
Then press configure interpreter, and navigate to the "Paths" tab.
Press the + button in the Paths area. You can put the path to the module you'd like it to recognize.
But I don't know the path..
Open the python interpreter where you can import the module.
>> import gnuradio
>> gnuradio.__file__
"path/to/gnuradio"
Most commonly you'll have a folder structure like this:
foobarbaz/
gnuradio/
__init__.py
other_file.py
You want to add foobarbaz to the path here.

You should never need to modify the path directly, either through environment variables or sys.path. Whether you use the os (ex. apt-get), or pip in a virtualenv, packages will be installed to a location already on the path.
In your example, GNU Radio is installed to the system Python 2's standard site-packages location, which is already in the path. Pointing PyCharm at the correct interpreter is enough; if it isn't there is something else wrong that isn't apparent. It may be that /usr/bin/python does not point to the same interpreter that GNU Radio was installed in; try pointing specifically at the python2.7 binary. Or, PyCharm used to be somewhat bad at detecting packages; File > Invalidate Caches > Invalidate and Restart would tell it to rescan.
This answer will cover how you should set up a project environment, install packages in different scenarios, and configure PyCharm. I refer multiple times to the Python Packaging User Guide, written by the same group that maintains the official Python packaging tools.
The correct way to develop a Python application is with a virtualenv. Packages and version are installed without affecting the system or other projects. PyCharm has a built-in interface to create a virtualenv and install packages. Or you can create it from the command line and then point PyCharm at it.
$ cd MyProject
$ python2 -m virtualenv env
$ . env/bin/activate
$ pip install -U pip setuptools # get the latest versions
$ pip install flask # install other packages
In your PyCharm project, go to File > Settings > Project > Project Interpreter. If you used virtualenvwrapper or PyCharm to create the env, then it should show up in the menu. If not, click the gear, choose Add Local, and locate the Python binary in the env. PyCharm will display all the packages in the selected env.
In some cases, such as with GNU Radio, there is no package to install with pip, the package was installed system-wide when you install the rest of GNU Radio (ex. apt-get install gnuradio). In this case, you should still use a virtualenv, but you'll need to make it aware of this system package.
$ python2 -m virtualenv --system-site-packages env
Unfortunately it looks a little messy, because all system packages will now appear in your env, but they are just links, you can still safely install or upgrade packages without affecting the system.
In some cases, you will have multiple local packages you're developing, and will want one project to use the other package. In this case you might think you have to add the local package to the other project's path, but this is not the case. You should install your package in development mode. All this requires is adding a setup.py file to your package, which will be required anyway to properly distribute and deploy the package later.
Minimal setup.py for your first project:
from setuptools import setup, find_packages
setup(
name='mypackage',
version='0.1',
packages=find_packages(),
)
Then install it in your second project's env:
$ pip install -e /path/to/first/project

For me, it was just a matter of marking the directory as a source root.

Add path in PyCharm 2017
File -> Settings (or Ctrl+Alt+S) -> Project -> Project Interpreter
Show all
Select bottom icon on the right side
Click on the plus button to add new path to your module

My version is PyCharm Professional edition 3.4, and the Adding a Path part is different.
You can go to "Preferences" --> "Project Interpreter". Choose the tool button at the right top corner.
Then choose "More..." --> "Show path for the selected interpreter" --> "Add". Then you can add a path.

DON'T change the interpreter path.
Change the project structure instead:
File -> Settings -> Project -> Project structure -> Add content root

In PyCharm 2020.1 CE and Professional, you can add a path to your project's Python interpreter by doing the following:
1) Click the interpreter in the bottom right corner of the project and select 'Interpreter Settings'
2) Click the settings button to the right of the interpreter name and select 'Show All':
3) Make sure your project's interpreter is selected and click the fifth button in the bottom toolbar, 'show paths for the selected interpreter':
4) Click the '+' button in the bottom toolbar and add a path to the folder containing your module:

For PyCharm Community Edition 2016.3.2 it is:
"Project Interpreter" -> Top right settings icon -> "More".
Then on the right side there should be a packages icon. When hovering over it it should say "Show paths for selected interpreter". Click it.
Then click the "Add" button or press "alt+insert" to add a new path.

As quick n dirty fix, this worked for me:
Adding this 2 lines before the problematic import:
import sys
sys.path.append('C:\\Python27\\Lib\site-packages')

On Project Explorer, you can right click on the folder where the module is contained and set as 'Source'.
It will be parsed in the Index for code completion as well as other items.

I'm new to PyCharm (using 2018.3.4 CE) and Python so I rotely tried to follow each of the above suggestions to access the PIL (Pillow) package which I knew was in system-site-packages. None worked. I was about to give up for the night when I happened to notice the venv/pyvenv.cfg file under my project in the Project Explorer window. I found the line "include-system-site-packages = false" in that file and so I changed it to "true". Problem solved.

In my PyCharm 2019.3, select the project, then File ---> Settings, then Project: YourProjectName, in 'Project Interpreter', click the interpreter or settings, ---> Show all... ---> Select the current interpreter ---> Show paths for the selected interpreter ---> then click 'Add' to add your library, in my case, it is a wheel package

For me there is also another issue. If you try to add a folder that in the past had a .idea folder, but your current project has it's own .idea folder your pycharm might get confused for some reason -- even if you have the right python/conda env. For me deleting the .idea folder of the other project fixed the confusion that it could find the obviously correctly installed pkgs. Then it was it was able to find them in the pycharm editor GUI snf stopped underlyinging them in red.

Download anaconda
https://anaconda.org/
once done installing anaconda...
Go into Settings -> Project Settings -> Project Interpreter.
Then navigate to the "Paths" tab and search for /anaconda/bin/python
click apply

Related

No module named 'facebook' [duplicate]

I'm having trouble with using 'requests' module on my Mac. I use python34 and I installed 'requests' module via pip. I can verify this via running installation again and it'll show me that module is already installed.
15:49:29|mymac [~]:pip install requests
Requirement already satisfied (use --upgrade to upgrade): requests in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages
Although I can import 'requests' module via interactive Python interpreter, trying to execute 'import requests' in PyCharm yields error 'No module named requests'. I checked my PyCharm Python interpreter settings and (I believe) it's set to same python34 as used in my environment. However, I can't see 'requests' module listed in PyCharm either.
It's obvious that I'm missing something here. Can you guys advise where should I look or what should I fix in order to get this module working? I was living under impression that when I install module via pip in my environment, PyCharm will detect these changes. However, it seems something is broken on my side ...
In my case, using a pre-existing virtualenv did not work in the editor - all modules were marked as unresolved reference (running naturally works, as this is outside of the editor's config, just running an external process (not so easy for debugging)).
Turns out PyCharm did not add the site-packages directory... the fix is to manually add it.
On Pycharm professional 2022.3
Open File -> Settings -> Python Interpreter, open the drop-down and pick "Show All..." (to edit the config) (1), right click your interpreter (2), click "Show Interpreter Paths" (3).
In that screen, manually add the "site-packages" directory of the virtual environment [looks like .../venv/lib/python3.8/site-packages (4) (I've added the "Lib" also, for a good measure); once done and saved, they will turn up in the interpreter paths.
The other thing that won't hurt to do is select "Associate this virtual environment with the current project", in the interpreter's edit box.
If you are using PyCharms CE (Community Edition), then click on:
File->Default Settings->Project Interpreter
See the + sign at the bottom, click on it. It will open another dialog with a host of modules available. Select your package (e.g. requests) and PyCharm will do the rest.
This issue arises when the package you're using was installed outside of the environment (Anaconda or virtualenv, for example). In order to have PyCharm recognize packages installed outside of your particular environment, execute the following steps:
Go to
Preferences -> Project -> Project Interpreter -> 3 dots -> Show All ->
Select relevant interpreter -> click on tree icon Show paths for the selected interpreter
Now check what paths are available and add the path that points to the package installation directory outside of your environment to the interpreter paths.
To find a package location use:
$ pip show gym
Name: gym
Version: 0.13.0
Summary: The OpenAI Gym: A toolkit for developing and comparing your reinforcement learning agents.
Home-page: https://github.com/openai/gym
Author: OpenAI
Author-email: gym#openai.com
License: UNKNOWN
Location: /usr/local/lib/python3.7/site-packages
...
Add the path specified under Location to the interpreter paths, here
/usr/local/lib/python3.7/site-packages
Then, let indexing finish and perhaps additionally reopen your project.
Open python console of your pyCharm. Click on Rerun.
It will say something like following on the very first line
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 52631 52632
in this scenario pyCharm is using following interpretor
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
Now fire up console and run following command
sudo /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -m pip install <name of the package>
This should install your package :)
Pycharm is unable to recognize installed local modules, since python interpreter selected is wrong. It should be the one, where your pip packages are installed i.e. virtual environment.
I had installed packages via pip in Windows. In Pycharm, they were neither detected nor any other Python interpreter was being shown (only python 3.6 is installed on my system).
I restarted the IDE. Now I was able to see python interpreter created in my virtual environment. Select that python interpreter and all your packages will be shown and detected. Enjoy!
Using dual python 2.7 and 3.4 with 2.7 as default, I've always used pip3 to install modules for the 3.4 interpreter, and pip to install modules for the 2.7 interpreter.
Try this:
pip3 install requests
This is because you have not selected two options while creating your project:-
** inherit global site packages
** make available to all projects
Now you need to create a new project and don't forget to tick these two options while selecting project interpreter.
The solution is easy (PyCharm 2021.2.3 Community Edition).
I'm on Windows but the user interface should be the same.
In the project tree, open External libraries > Python interpreter > venv > pyvenv.cfg.
Then change:
include-system-site-packages = false
to:
include-system-site-packages = true
Before going further, I want to point out how to configure a Python interpreter in PyCharm: [SO]: How to install Python using the "embeddable zip file" (#CristiFati's answer). Although the question is for Win, and has some particularities, configuring PyCharm is generic enough and should apply to any situation (with minor changes).
There are multiple possible reasons for this behavior.
1. Python instance mismatch
Happens when there are multiple Python instances (installed, VEnvs, Conda, custom built, ...) on a machine. Users think they're using one particular instance (with a set of properties (installed packages)), but in fact they are using another (with different properties), hence the confusion. It's harder to figure out things when the 2 instances have the same version (and somehow similar locations)
Happens mostly due to environmental configuration (whichever path comes 1st in ${PATH}, aliases (on Nix), ...)
It's not PyCharm specific (meaning that it's more generic, also happens outside it), but a typical PyCharm related example is different console interpreter and project interpreter, leading to confusion
The fix is to specify full paths (and pay attention to them) when using tools like Python, PIP, .... Check [SO]: How to install a package for a specific Python version on Windows 10? (#CristiFati's answer) for more details
This is precisely the reason why this question exists. There are 2 Python versions involved:
Project interpreter: /Library/Frameworks/Python.framework/Versions/3.4
Interpreter having the Requests module: /opt/local/Library/Frameworks/Python.framework/Versions/3.4
well, assuming the 2 paths are not somehow related (SymLinked), but in latest OSX versions that I had the chance to check (Catalina, Big Sur, Monterey) this doesn't happen (by default)
When dealing with this kind of error, it always helps (most likely) displaying the following information (in a script or interpreter console):
import os
import sys
print(sys.executable)
print(sys.version)
print(os.getcwd())
print(getattr(os, "uname", lambda: None)())
print(sys.path)
2. Python's module search mechanism misunderstanding
According to [Python.Docs]: Modules - The Module Search Path:
When a module named spam is imported, the interpreter first searches for a built-in module with that name. These module names are listed in sys.builtin_module_names. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
The installation-dependent default (by convention including a site-packages directory, handled by the site module).
A module might be located in the current dir, or its path might be added to ${PYTHONPATH}. That could trick users into making them believe that the module is actually installed in the current Python instance ('s site-packages). But, when running the current Python instance from a different dir (or with different ${PYTHONPATH}) the module would be missing, yielding lots of headaches
For a fix, check [SO]: How PyCharm imports differently than system command prompt (Windows) (#CristiFati's answer)
3. A PyCharm bug
Not very likely, but it could happen. An example (not related to this question): [SO]: PyCharm 2019.2 not showing Traceback on Exception (#CristiFati's answer)
To fix, follow one of the options from the above URL
4. A glitch
Not likely, but mentioning anyway. Due to some cause (e.g.: HW / SW failure), the system ended up in an inconsistent state, yielding all kinds of strange behaviors
Possible fixes:
Restart PyCharm
Restart the machine
Recreate the project (remove the .idea dir from the project)
Reset PyCharm settings: from menu select File -> Manage IDE Settings -> Restore Default Settings.... Check [JetBrains]: Configuring PyCharm settings or [JetBrains.IntelliJ-Support]: Changing IDE default directories used for config, plugins, and caches storage for more details
Reinstall PyCharm
Needless to say that the last 2 options should only be attempted as a last resort, and only by experts, as they might mess up other projects and not even fix the problem
Not directly related to the question, but posting:
[SO]: Run / Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (a PyCharm related investigation from a while ago)
[SO]: ImportError: No module named win32com.client (#CristiFati's answer)
If you go to pycharm project interpreter -> clicked on one of the installed packages then hover -> you will see where pycharm is installing the packages. This is where you are supposed to have your package installed.
Now if you did sudo -H pip3 install <package>
pip3 installs it to different directory which is /usr/local/lib/site-packages
since it is different directory from what pycharm knows hence your package is not showing in pycharm.
Solution: just install the package using pycharm by going to File->Settings->Project->Project Interpreter -> click on (+) and search the package you want to install and just click ok.
-> you will be prompted package successfully installed and you will see it pycharm.
If any one faces the same problem that he/she installs the python packages but the PyCharm IDE doesn't shows these packages then following the following steps:
Go to the project in the left side of the PyCharm IDE then
Click on the venv library then
Open the pyvenv.cfg file in any editor then
Change this piece of code (include-system-site-packages = flase) from false to true
Then save it and close it and also close then pycharm then
Open PyCharm again and your problem is solved.
Thanks
This did my head in as well, and turns out, the only thing I needed to do is RESTART Pycharm. Sometimes after you've installed the pip, you can't load it into your project, even if the pip shows as installed in your Settings. Bummer.
For Anaconda:
Start Anaconda Navigator -> Enviroments -> "Your_Enviroment" -> Update Index -> Restart IDE.
Solved it for me.
After pip installing everything I needed. I went to the interpreter and re-pointed it back to where it was at already.
My case: python3.6 in /anaconda3/bin/python using virtualenv...
Additionally, before I hit the plus "+" sign to install a new package. I had to deselect the conda icon to the right of it. Seems like it would be the opposite, but only then did it recognize the packages I had/needed via query.
In my case the packages were installed via setup.py + easy_install, and the they ends up in *.egg directories in site_package dir, which can be recognized by python but not pycharm.
I removed them all then reinstalled with pip install and it works after that, luckily the project I was working on came up with a requirements.txt file, so the command for it was:
pip install -r ./requirement.txt
I just ran into this issue in a brand new install/project, but I'm using the Python plugin for IntelliJ IDEA. It's essentially the same as PyCharm but the project settings are a little different. For me, the project was pointing to the right Python virtual environment but not even built-in modules were being recognized.
It turns out the SDK classpath was empty. I added paths for venv/lib/python3.8 and venv/lib/python3.8/site-packages and the issue was resolved. File->Project Structure and under Platform Settings, click SDKs, select your Python SDK, and make sure the class paths are there.
pip install --user discord
above command solves my problem, just use the "--user" flag
I fixed my particular issue by installing directly to the interpreter. Go to settings and hit the "+" below the in-use interpreter then search for the package and install. I believe I'm having the issue in the first place because I didn't set up with my interpreter correctly with my venv (not exactly sure, but this fixed it).
I was having issues with djangorestframework-simplejwt because it was the first package I hadn't installed to this interpreter from previous projects before starting the current one, but should work for any other package that isn't showing as imported. To reiterate though I think this is a workaround that doesn't solve the setup issue causing this.
If you are having issues with the underlying (i.e. pycharm's languge server) mark everything as root and create a new project. See details: https://stackoverflow.com/a/73418320/1601580 this seems to happy to me only when I install packages as in editable mode with pip (i.e. pip install -e . or conda develop). Details: https://stackoverflow.com/a/73418320/1601580
--WINDOWS--
if using Pycharm GUI package installer works fine for installing packages for your virtual environment but you cannot do the same in the terminal,
this is because you did not setup virtual env in your terminal, instead, your terminal uses Power Shell which doesn't use your virtual env
there should be (venv) before you're command line as shown instead of (PS)
if you have (PS), this means your terminal is using Power Shell instead of cmd
to fix this, click on the down arrow and select the command prompt
select command prompt
now you will get (venv) and just type pip install #package name# and the package will be added to your virtual environment
On windows I had to cd into the venv folder and then cd into the scripts folder, then pip install module started to work
cd venv
cd scripts
pip install module
instead of running pip install in the terminal -> local use terminal -> command prompt
see below image
pycharm_command_prompt_image
In your pycharm terminal run pip/pip3 install package_name

Already Installed package on cmd then how it work on pycharm?

I want to install one package on cmd then it also work on pycharm. No need to download or install it from pycharm. How it is possible? Please help me. I already installed that package on cmd.
Adding a Path
Go into File > Settings > Project Settings > Project Interpreter.
Then press configure interpreter, and navigate to the "Paths" tab.
pycharm path tab
Press the + button in the Paths area. You can put the path to the module you'd like it to recognize.
But I don't know the path..
Open the python interpreter where you can import the module.
>> import package_name
>> package_name.__file__
"path/to/package_name"
Most commonly you'll have a folder structure like this:
foobarbaz/
Package_name/
__init__.py
other_file.py
You want to add foobarbaz to the path here.
Assumption:
Initial installation of Python, Add Python to Path . was checked.
Initial installation of PyCharm.
A package is installed using command line pip install package.
Result:
Package is installed in the default installation of Python.
Next:
Start a new PyCharm project.
Select File menu |Settings
Check the Base interpreter, it will likely be the python.exe file in the default Python installation. If not, locate the python.exe file and select it.
Check Inherit global-site packages.
Click Create Button
Result: The project will be created with installed packages.

How to I fix the “ModuleNotFound” error when importing PyQt5? [duplicate]

I installed bottle on Python 3.4 with pip install. In the terminal, when I do:
$ python3.4
>>>import bottle # shows no import error
>>>
but when I do it in PyCharm, it says:
import bottle ImportError: No module named 'bottle'
in your PyCharm project:
press Ctrl+Alt+s to open the settings
on the left column, select Project Interpreter
on the top right there is a list of python binaries found on your system, pick the right one
eventually click the + button to install additional python modules
validate
In some cases no "No module ..." can appear even on local files. In such cases you just need to mark appropriate directories as "source directories":
The settings are changed for PyCharm 5+.
Go to File > Default Settings
In left sidebar, click Default Project > Project Interpreter
At bottom of window, click + to install or - to uninstall.
If we click +, a new window opens where we can decrease the results by entering the package name/keyword.
Install the package.
Go to File > Invalidate caches/restart and click Invalidate and Restart to apply changes and restart PyCharm.
Settings:
Install package:
I am using Ubuntu 16.04. For me, it was the incorrect interpreter, which was by default using the virtual interpreter from the project.
So, make sure you select the correct one, as the pip install will install the package to the system Python interpreter.
PyCharm 2019.3, my solution is below:
For me, none of the above worked, and curiously even within one file some imports worked, some didn't:
from folder1.folder2.folder3.my_python_file import this_function # worked
from folder1.folder2.folder3.my_python_file import that_function # didn't work
Follow the above advice, but if it doesn't fix it additionally, (in PyCharm) click File >> Repair IDE and confirm all the 6 steps one after another.
I had virtual env site package problem and this solved it:
In the case where you are able to import the module when using the CLI interpreter but not in PyCharm, make sure your project interpreter in PyCharm is set to an actual interpreter (eg. /usr/bin/python2.7) and not venv (~/PycharmProject/venv/...)
I had the same problem, I tried all fixes like installing from the project interpreter and installing from python console, nothing worked. What worked was just going to the project folder from the terminal and installing it from there.

How to make pygame on available in an virtualenv? [duplicate]

Pycharm doesn't sync with the pip packages though i have added the path in environment variable.
The following elements will clear the topic:
already installed sklearn package:
pycharm can't find that package:
environment variable:
The thing is that pycharm creates all of your projects inside a virtual environment. So the packages that you installed outside the virtual environment(global site packages) does not get inherited in to your project. There is a simple solution for that. When you create a pycharm project, make sure to check the "inherit global-site packages" checkbox as shown here.Then you'll be able to import all the packages to your virtual environment, provided they have been installed in your computer.
Alternatively you can install all the packages separately using "pip install" in the pycharm terminal. If the package has been installed outside the virtual environment, pip will use cached files instead of downloading the package again.
Or you can install the packages using Settings -> Project -> Project Interpreter as the other answers suggests.
In PyCharm, go to File / Default Settings / Project Interpreter
On that page there is a plus in the bottom left, whatever modules you have installed through pip you may need to manually install there. Or you can change your project interpreter to make sure you are using the correct interpreter with all your installed modules.
Seems like the interpreter selected isn't where you think it is. If you're using a different interpreter than the system's default, it won't load the modules. To add the modules in PyCharm, go to your Settings, Project Interpreter, the Add Button, and install it with PyCharm's integrated package management.
If you did not "inherit global-site packages" upon creating the project, check in the project tree under External Libaries, if the folder is marked in red. I.e. if you do not use the systems default interpreter , Lib/site-packages could be marked in red.
To add those libraries afterwards, go to: File-Project(your project)\Project Structure,
select +Add Content Root, browse to site-packages (or the respective folder you want to add), and add it. After clicking OK the folder will not be marked red any longer. (tested in PyCharm 2020.3)

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?
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)
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:
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.
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.
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.
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.
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
Scrin1
Select the directory where you installed the libraries. In my case, this is "lib".
Mark the directory as "source".
Scrin2
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.
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.

Categories