pytest: getting AttributeError: 'CaptureFixture' object has no attribute 'readouterror' capturing stdout [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
This should be a simple thing but I can't figure out what is causing the error.
I'm following the pytest docs on how to capture stdout into an object but am getting the following error:
capsys = <_pytest.capture.CaptureFixture object at 0x7f02a1e2f7f0>
def test_can_output_to_stdout(capsys):
print("hello")
> capture = capsys.readouterror()
E AttributeError: 'CaptureFixture' object has no attribute 'readouterror'
test_aaa.py:5: AttributeError
The code I'm using is similar to:
import pytest
def test_can_output_to_stdout(capsys):
print("hello")
capture = capsys.readouterror()
assert "hello" in capture.out
I'm calling the test like so:
py.test --capture=sys --capture=fd test_aaa.py
The versions are:
pytest:
py.test --version
This is pytest version 4.6.5, imported from /usr/local/lib/python3.4/site-packages/pytest.p
Python:
python --version
Python 3.4.8
Any help would be greatly appreciated.

It turns out copy pasting is probably better than writing out the examples. The error was with the attribute name.
It should be capsys.readouterr(), not capsys.readouterror(), and in full:
import pytest
def test_can_output_to_stdout(capsys):
print("hello")
capture = capsys.readouterr()
assert "hello" in capture.out

Related

Why aren't I able to perform simple arithmetic operations in python using Vscode but I can elsewhere [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
essentially when I run something as simple as '3 * 3' in any other IDE I have no issues, however, all arithmetic operations in my Vscode seem to return nothing, the terminal just repeats my PWD essentially.
Note that I am new and learning python but everything else thus far has worked fine, print statements print to the terminal etc but maths docent seem to work.
enter code here: def example(val1, val2):
return val1 * val2
example(2, 6)
Edit: by terminal I mean I am writing the script and clicking the run button in vscode which outputs to said terminal, I have uploaded a SC of what I mean :screenshot of the IDE and terminal output
In some environments, such as the Python REPL (the P stands for "print") and Jupyter Notebook), results are printed out automatically. However, when you run a script from the command line or VS Code, printing isn't automatic. To output something in this case, you need to add a print() command:
def example(val1, val2):
return val1 * val2
result = example(2, 6)
print(result)
Your terminal is running a shell, probably bash not python.
Then, if you want to evaluate that in bash do
$(( 3 * 3 ))

Python 3: AttributeError: 'int' object has no attribute 'choice' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Trying to run this python 3 program but it seems it can't run past the first loop.
import webbrowser
import time
import random
while True:
sites = ['www.google.com', 'www.mastercode.com', 'www.youtube.com']
site = random.choice(sites)
visit = ("http://{}".format(site))
webbrowser.open(vist)
time.sleep(2)
After the 1st loop it gives this error:
AttributeError: 'int' object has no attribute 'choice'
I am unable to fix it.I am still new to python 3.
I think you must have a variable called random, I mean you had override the default random module of python, change that variable name from random to something else.

Script running on Linux gives NameError [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I have a python script running on a Linux server. The code:
#!/bin/python
databaseRun = input("Do you want to run all databases: ")
if databaseRun == "yes":
print("yes")
else:
print("no")
This returns and error:
Traceback (most recent call last):
File "./db_upg.py", line 3, in <module> databaseRun = input("Do you want to run all databases: ")
File "string", line 1, in <module> NameError: name 'yes' is not defined
Now if I type this code into PyCharm it runs with no problem. What am I missing? Basically you will put yes or no will be the only to answers
You're probably running this with Python 2 instead of Python 3. Try running it with python3 <scriptname> instead of python <scriptname>. And change the shebang line from #!/bin/python to #!/usr/bin/python3 respectively.
In Python 2, input() reads the input as a code instead of a string. Thus when you type yes, it reads it as a variable yes which is undefined. If you want to run this with Python 2 instead of Python 3 as your tag suggests, use raw_input() instead of input(). Python 2's raw_input() returns a string, similar to Python 3's input().

NameError: name 'ftplip' is not defined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Following is my python code to make a FTP connection
upload_ftp.py
import ftplib
ftp = ftplip.FTP()
ftp.connect('ip', 21)
print ftp.getwelcome()
try:
print "Logging in..."
ftp.login("username", "password")
except:
"failed to login"
but when i run the code ,i get the following error: NameError:name 'ftplib' is not defined
my#my-pc:/var/www$ python upload_ftp.py
Traceback (most recent call last):
File "upload_ftp.py", line 8, in <module>
ftp1 = ftplip.FTP()
NameError: name 'ftplip' is not defined
Any help would be appreciated..
You have a spelling error in your variable name. So Python thinks you are using an undefined variable.
You have a typo: you writing ftplip.FTP() but it is ftplib.FTP() (lip with p vs lib with b).
Use :
from ftplib import FTP
ftp = FTP()
Return a new instance of the FTP class.

Python, got an unexpected keyword argument in my python code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My python code keeps giving me this error
this is the function I try to call with underneath it the code that calls it.
from sys import stdout
def print_nested_list(lijst, indent=False, indent_level=0, fh=stdout):
for x in lijst:
if isinstance(x, list):
print_nested_list(x, indent, indent_level+1, fh)
else:
if indent:
for tabstop in range(indent_level):
print("\t", end='', file=fh)
print(x, file=fh)
try:
with open(r'C:\Python34\headfirstpython\chapter 3\man_data.txt', 'w') as man_data:
print_nested_list(man, fh=man_data)
with open(r'C:\Python34\headfirstpython\chapter 3\other_data.txt', 'w') as other_data:
print_nested_list(other, fh=other_data)
IDLE gives this error when i try to run it
Traceback (most recent call last):
File "C:\Python34\headfirstpython\chapter 3\sketch1.py", line 25, in <module>
print_nested_list(man, fh=man_data)
TypeError: print_nested_list() got an unexpected keyword argument 'fh'
I don't understand what is wrong with my function or the way I call my function?
In the argument list, you don't have 'fh' - you have 'fh_id'. Try using 'fh' instead.
Your function doesn't have a fh keyword argument. It has a fh_id keyword argument though.
Either fix your function signature (rename fh_id to fh) or your call (use fh_id instead of fh).

Categories

Resources