6

I tried to simply read and store the contents of a text file into an array, but:

ins = open( "file.txt", "r" )
array = []
for line in ins:
    array.append( line )
ins.close()

It gives me an error for "open":

Unresolved reference 'open' less... (Ctrl+F1) 

This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

Can anyone explain what I'm doing wrong? Thanks

TheSoundDefense
  • 6,753
  • 1
  • 30
  • 42
Évariste Galois
  • 1,043
  • 2
  • 13
  • 27
  • What are you using to write this script? Notepad, some IDE? What are you using to run it? – Kevin Aug 05 '14 at 19:57
  • My IDE is PyCharm, I just started with it so I am unfamiliar. – Évariste Galois Aug 05 '14 at 19:58
  • 1
    This seems to be a common-ish problem with PyCharm, take a look here: http://stackoverflow.com/questions/21236824/unresolved-reference-issue-in-pycharm – Chrispresso Aug 05 '14 at 20:03
  • It must be your IDE set up. The code you posted works just fine as is; I tried it interactively in Idle. – ScottO Aug 05 '14 at 20:04
  • @user25997098, I looked at the answer for that, and I did follow the steps. However, I still need to know the proper import to use for reading/writing files with PyCharm. – Évariste Galois Aug 05 '14 at 20:13
  • Have you done this? - http://confluence.jetbrains.com/display/PYH/Configuring+Interpreters+with+PyCharm?_ga=1.184345518.948537921.1407270133 PyCharm has to know where the version of python you want to use is located on your system. – ScottO Aug 05 '14 at 20:25
  • This issue will get resolved after IDE restart – Mayur Aug 16 '18 at 09:16

3 Answers3

4

This is a known issue in PyCharm, when it fails to update its cache of the interpreter. It happens most often if you install a new interpreter, update packages, etc.

You can search for this and related issues at the pycharm bug tracker

Its a temporary problem and will resolve itself. Keep an eye on the bottom right of the PyCharm window where it displays notifications (next to the icon of the guy in the hat). Click on this and the Event Log will have some messages for you.

If its really bothering you, you can hit ALT+ENTER and click "Ignore unresolved reference open"

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
1

Have you checked your preferences to make sure that you are using the correct python interpreter? ie define which version of python pyCharm should be using? This is set in your preferences:

Ctl+Shift+a (cmd+shift+a on Mac) and type "project interpreter", then set this to be python 2.x or 3.x depending on what you have installed(or whatever virtualEnv you have defined).

Then you can invalidate the cache and restart.

Lingster
  • 977
  • 10
  • 21
-2
array = []

with open('/path/to/file', 'r') as fp:
    for line in fp.readlines():
        array.append(line)
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43