Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python scratch files in Pycharm - modules not being found?

Tags:

python

pycharm

I'm new to Python and using PyCharm professional as my IDE. I have a small section of code that I want to work with from a longer file, so I created a "scratch file" with Python set as the interpreter. However, even just with importing modules I'm getting errors that the modules can't be found (even with standard modules). The file is set as a "Python" scratch file, so I'm not sure what else I need to do. The code I'm trying to run is:

from zipfile import ZipFile
import urllib

testfile = urllib.request.urlretrieve("https://ihmecovid19storage.blob.core.windows.net/latest/ihme-covid19.zip", "ihme-covid19.zip")
print("File saved at: " + (str(os.getcwd())))

with ZipFile('ihme-covid19.zip', 'r') as zipobj:
    print(zipobj.printdir())
    zipobj.extractall()

Everything is showing up with an error - no module urllib, no module zipfile, etc.

like image 554
user13132640 Avatar asked Jun 30 '26 07:06

user13132640


1 Answers

This may come across like a non-answer, but I do think this is a bug in Pycharm.

I even downloaded an update, restarted pycharm, and rebuilt my indexes. Same thing, here -- core Python modules are not registering as being importable (i.e. import sys or import inspect both failing type-checking).

enter image description here

We should probably move this to Pycharm's bug tracker. For example:

  • Here is a similar issue around a specific import NamedTuple
  • Here's an issue around the default interpreter in scratch files

There may be a better existing bug ticket, so please do look and report back here -- I'll update this comment if someone finds or creates a better bug ticket on youtrack.jetbrains.com and shares the link.

like image 143
Lotus Avatar answered Jul 02 '26 19:07

Lotus