PyCharm Fails to resolve references to installed modules and even built-ins

Answered
from __future__ import print_function
from builtins import input as input
import getpass

from sqlalchemy.exc import OperationalError
from terminaltables import SingleTabl

I am using a virtualenv generated by PyCharm itself. I have tried using my own virtualenv, a pipenv interpreter, and even the base python interpreter.

I have tried uninstalling, deleting my .idea folder, deleting the files in %appdata%, deleting the install directory, and reinstalling. I then set up a new virtualenv and installed packages. I have tried every option in the Project Interpreter outside of Conda. I have tried invalidating caches and restarting.

PyCharm still does not resolve references to *any* installed packages, and cannot find some built-in packages such as json and getpass.

 

4
16 comments
Avatar
Permanently deleted user

For the above snippet, the following errors are occuring:

unresolved reference __future__

unresolved reference print_function

No module named getpass

unresolved reference sqlalchemy

unresolved reference OperationalError

unresolved reference terminaltables

unresolved reference SingleTable

 

 

0
Avatar
Permanently deleted user

I would like to change this issue to a new Bug Report.

PyCharm does not set the Content Root to the site-packages directory when setting the project interpreter.

This wasted almost an entire day. Please add this to the Very Important bug list.

2

Hi!

It seems you have found the root cause for the issue. Can you please provide a full steps to reproduce the issue from scratch? Starting from creating a new project.

The following steps have NOT reproduced the issue for me:

1. Create new project with the new Virtualenv

2. Paste the example from above

3. Install sqlalchemy and terminaltables modules, and wait for PyCharm to index

4. Unresolved error does not apper

0
Avatar
Permanently deleted user

Hi,

This was a reoccurring issue up until I set the Content-Root. Every time PyCharm would update itself with the 'elevator' this would happen. I would try to break it again but I prefer not to waste time with this bug anymore.

0

Understood. Yes, things like that can happen between updates, I'll keep in mind to check content root if someone else will have the same issue.

0
Avatar
Permanently deleted user

A similar issue here.

from __future__ import print_function

Unresolved reference '__future__'
Unresolved reference 'print_function'

There is a similar issue with half of standard library modules, too:

import zipfile

Unresolved reference 'zipfile'

It seems to have started with some update, I have no other details.

0

@Peter

Does not reproduce for me. I'm testing on PyCharm 2019.1.3, and tried both Python 2 and Python 3.

Can you please do the usual troubleshooting steps of **File > Invalidate caches / restart**, and/or deleting and recreating your project interpreter? Which interpreter do you use?

0
Avatar
Permanently deleted user

Andrey,

I apologize for filing my report as it was not justified. Due to some company-wide decisions I am using an interpreter wrapper (a simple Bash script which modifies some environment variables and launches the python executable with correct arguments). Setting this wrapper as my Project Interpreter in Settings -> Project -> Project Interpreter makes PyCharm recognize only some of the standard library modules - other modules are underlined as unrecognized. I was mistakenly thinking that this strange behavior was a result of some past PyCharm update a I tried to find a similiar bug report here on intellij-support.

However, could you advice me on how PyCharm learns the system paths related to the selected interpreter? If it is by calling the interpreter somehow maybe I could modify my wrapper to pass the correct results.

Thank you,
Peter

0

PyCharm constructs interpreter paths based on the sys.path of the given interpreter. Doesn't IDE report your interpreter as invalid? Which entries do you have in interpreter paths?

1
Avatar
Permanently deleted user

I've also had this problem in PyCharm. The problem for me was that my virtual envs were using a version of python which got removed when I updated Python using Homebrew. The virtual envs contained sym links to python files which included the patch version in the directory names.

To fix this I deleted all the sym links in the virtual env and recreated them, following one of the answers to this question on stack overflow.

1
Avatar
Permanently deleted user

Managed to replicate this issue. Created a new project with a venv, pip installed all the external libraries needed for the project, and then wrote all the files with the python code.

How it was solved was that I again created a new project with a venv. Wrote all the files with python code first and then pip installing all the external modules needed. 

0
Avatar
Permanently deleted user

I have several systems and this comes and goes.  I get a situation where every 'dict',' len' 'Exception' is underlined and flagged.  If I restart pycharm it goes away about 60% of the time.  I don't really use the configured python in pycharm otherwise but have set it up and it shows no warnings, gives a python prompt if you open a the window, etc.

This worked fine on several machines until recently.  I suspect a recent pycharm update a month or so ago.  This is an editor/IDE's job.  It shouldn't be this flaky.  I'm thinking atom, or notepad.  

0
Avatar
Permanently deleted user

I found another way how PyCharm is failing to resolve python code.

 

This was an enlightening post: https://www.programmersought.com/article/80394814054/

The issue is that files __init__.py are not recognized as python code... ^^'

 

When library uses the following pattern to flatten module structure:

in __init__.py there is code:

__all__ = [
"ComputeTarget",

Then Class ComputeTarget might be not visible to PyCharm despite the fact code compiles fine.

 

Fix as mention in the source:

File→Settings→Editor→File Types→Python 

add __init__,py as python file..

 

2
Avatar
Permanently deleted user

@Jkonczak, this did fix it for me!

Shouldn't this be the default behaviour, i.e. it is thus a bug? 

At least the fix was quite easy.

0
Avatar
Permanently deleted user

@...

I would call this a bug for sure since the __init__.py is for sure the python code... :) Although I got no idea where to repot this... 

0

@... Thanks for your answer, It's really helpful !!!

0

Please sign in to leave a comment.