2

I'm new to python, been learning it for the past few weeks. I recently started getting the following error when debugging a Python script in VS Code:

Exception has occurred: AttributeError
'NoneType' object has no attribute 'write'
  File "C:\SW-Languages\__LEARNING\Learning Python\Ex_Files_Learning_Python\Exercise Files\Ch3\itertoolsPermutations.py", line 8, in <module>
    print("Hello")

Here is the code (originally was more, but isolated it to this simple 1 line program:

print("Hello")

I installed VS Code from Anaconda, everything worked fine for the first couple of weeks, then I made an updates to anaconda (I don't recall details) and also installed new Python packages when the error first occurred.

The "Hello" script works fine when executed from a command shell (as do all my other python scripts, but basically I can only use VS Code for editing, none of my scripts will run in VS Code now, I have to execute them via a cmd shell.

Additional info:

from cmd shell:

C:\Users\jd400>python -VV

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]

from VS Code terminal:

(base) PS C:\SW-Languages\__LEARNING\wxFormBuilder\wx-Widget-Examples\sc> python -VV

Python 3.7.2 (default, Feb 21 2019, 16:05:07) [MSC v.1915 64 bit (AMD64)]

I have researched the following related posts on stackoverflow, but they were no help:

How do I resolve 'NoneType' object has no attribute 'write' error with scikit-learn digits dataset?

AttributeError: 'NoneType' object has no attribute 'write'

Don't understand what this AttributeError means

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    You do not execute a 1-line program and get that error message: `print("Hello")` works. Please post a [mcve] that lets us reproduce your error. – Patrick Artner Mar 22 '19 at 13:03
  • Sorry but the entire program consists of 1 line: print ("Hello") and nothing else. The original code had more, but I commented out all but the print statement. In fact, I just created a new script with a one line print statement and still get the same error : – user1995818 Mar 22 '19 at 13:20
  • Your error is in `C:\SW-Languages\__LEARNING\Learning Python\Ex_Files_Learning_Python\Exercise Files\Ch3\itertoolsPermutations.py` wich at least contains 8 lines according to the error message. You other program with print("hello") has nothing to do with it. Copy & paste your program into an online IDE - f.e. http://pyfiddle.io - put it to python 3.x and execute it there. – Patrick Artner Mar 22 '19 at 13:22
  • Yes line 8 is this: "print("Hello")" - print statements used to work, but are no longer working in VS Code. But when I execute this script from a cmd shell it works fine. – user1995818 Mar 22 '19 at 13:26
  • My issue is with VS Code, I can no longer debug in it. Print statements no longer work as mentioned above. Print statements cause this exception: Exception has occurred: AttributeError 'NoneType' object has no attribute 'write' – user1995818 Mar 22 '19 at 13:37
  • 1
    You have two different versions of Python 3.7 installed on your system. One in a 32-bit Python 3.7.0 and the other that VS Code is using is 64-bit Python 3.7.2. It quite possible that some extensions that work in one don't work in the other—so I suggest fix that issue by getting rid of one of them. Also, FWIW, one way that `print()` calls could producean ` AttributeError` like that is if `sys.stdout` has somehow been set to `None`. – martineau Mar 22 '19 at 15:44

1 Answers1

0

I found the solution after doing more research on the web -

https://github.com/Microsoft/vscode-python/issues/3272 Simple print command not working #3272

The solution is to change python executable in settings.json from: ...\Anaconda3\pythonw.exe", to ...\Anaconda3\python.exe",

I appreciate the suggestions others have made in solving my issue and even though it's now solved, I still may get rid of the other installed version of python as martineau suggested to make things cleaner.