'Context' object has no attribute 'stdout_capture' - python

I'm new to python and bdd and need some help, please. I'm trying to get junit reports from python behave, but after each test I got this error:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\behave_runner.py", line 271, in <module>
_BehaveRunner(my_config, base_dir).run()
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\_bdd_utils.py", line 96, in run
self._run_tests()
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\behave_runner.py", line 189, in _run_tests
self.__real_runner.run()
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\runner.py", line 672, in run
return self.run_with_paths()
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\runner.py", line 693, in run_with_paths
return self.run_model()
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\behave_runner.py", line 112, in run_model
return super(_RunnerWrapper, self).run_model(features)
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\runner.py", line 483, in run_model
failed = feature.run(self)
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\model.py", line 523, in run
failed = scenario.run(runner)
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\model.py", line 914, in run
self.stdout = runner.context.stdout_capture.getvalue()
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\runner.py", line 214, in __getattr__
raise AttributeError(msg)
AttributeError: 'Context' object has no attribute 'stdout_capture'
By the way flags in behave.ini are next:
[behave]
junit=True
stdout_capture=True
stderr_capture=True

I solve this. If you use ide to run test, as I do (PyCharm), then try to run it from command line and all should work fine.

Related

Miniconda: cannot launch spyder

I have always been able to launch spyder without any issues. But today I started running into the following error messages. I don't understand why is it complaining no workingdir? How do I resolve this, as I cannot even launch spyder ?
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\Scripts\spyder-script.py", line 10, in <module>
sys.exit(main())
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\app\start.py", line 253, in main
mainwindow.main(options, args)
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\app\mainwindow.py", line 1956, in main
mainwindow = create_window(MainWindow, app, splash, options, args)
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\app\utils.py", line 289, in create_window
main.setup()
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\app\mainwindow.py", line 771, in setup
PLUGIN_REGISTRY.register_plugin(self, PluginClass,
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\api\plugin_registration\registry.py", line 342, in register_plugin
instance = self._instantiate_spyder5_plugin(
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\api\plugin_registration\registry.py", line 193, in _instantiate_spyder5_plugin
plugin_instance.initialize()
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\api\plugins\new_api.py", line 669, in initialize
self.on_initialize()
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\plugins\workingdirectory\plugin.py", line 82, in on_initialize
container.set_history(
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\plugins\workingdirectory\container.py", line 323, in set_history
workdir = self._get_init_workdir()
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\plugins\workingdirectory\container.py", line 183, in _get_init_workdir
workdir = self.get_conf('startup/fixed_directory')
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\api\config\mixins.py", line 76, in get_conf
return CONF.get(section, option, default)
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\config\manager.py", line 499, in get
value = config.get(section=section, option=option, default=default)
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\config\user.py", line 980, in get
return config.get(section=section, option=option, default=default)
File "C:\Users\xxx\AppData\Local\miniconda\envs\py3_10\lib\site-packages\spyder\config\user.py", line 511, in get
raise cp.NoSectionError(section)
configparser.NoSectionError: No section: 'workingdir'

win32com EnsureDispatch fails to load DLL at creating gen_py cache

My python code raises an error when creating the necessary gen_py folder in the \Appdata\Local\Temp\ directory. The error is raised when executing the following line:
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
When executing this line (for the first time) the following error is raised:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 531, in EnsureDispatch
mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 515, in EnsureModule
module = MakeModuleForTypelib(typelibCLSID, lcid, major, minor, progressInstance, bForDemand = bForDemand, bBuildHidden = bBuildHidden)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 284, in MakeModuleForTypelib
makepy.GenerateFromTypeLibSpec( (typelibCLSID, lcid, major, minor), progressInstance=progressInstance, bForDemand = bForDemand, bBuildHidden = bBuildHidden)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\makepy.py", line 279, in GenerateFromTypeLibSpec
gen.generate(fileUse, bForDemand)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 822, in generate
self.do_generate()
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 885, in do_generate
oleItems, enumItems, recordItems, vtableItems = self.BuildOleItemsFromType()
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 750, in BuildOleItemsFromType
oleItem, vtableItem = self._Build_Interface(type_info_tuple)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 718, in _Build_Interface
oleItem = DispatchItem(info, attr, doc)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 270, in __init__
build.DispatchItem.__init__(self, typeinfo, attr, doc)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\build.py", line 115, in __init__
self.Build(typeinfo, attr, bForUser)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\build.py", line 262, in Build
fdesc = typeinfo.GetFuncDesc(j)
pywintypes.com_error: (-2147312566, 'Fout bij laden van type DLL-bestand.', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python\file.py", line 659, in <module>
start()
File "D:\Python\file.py", line 55, in start
AE_addPivotTables(str_filename, df_opzet)
File "D:\Python\file.py", line 556, in AE_addPivotTables
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 538, in EnsureDispatch
raise TypeError("This COM object can not automate the makepy process - please run makepy manually for this object")
TypeError: This COM object can not automate the makepy process - please run makepy manually for this object
Manually running the makepy process raises the same error.
After this error I see in the directory \Appdata\Local\Temp\ that the folder 'gen_py' has been created with the expected files in the cache. However my code is not dispatching excel yet because when I run the script a second time the following error is raised:
Traceback (most recent call last):
File "D:\Python\file.py", line 659, in <module>
start()
File "D:\Python\file.py", line 55, in start
AE_addPivotTables(str_filename, df_opzet)
File "D:\Python\file.py", line 556, in AE_addPivotTables
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application') # Excel = win32com.client.Dispatch('Excel.Application')
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 531, in EnsureDispatch
mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 388, in EnsureModule
module = GetModuleForTypelib(typelibCLSID, lcid, major, minor)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 263, in GetModuleForTypelib
AddModuleToCache(typelibCLSID, lcid, major, minor)
File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 549, in AddModuleToCache
dict = mod.CLSIDToClassMap
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'CLSIDToClassMap'
It seems that the first error raised when creating the gen_py folder results in a cache that cannot be used. The problem seems to be in loading DLL files by pywintypes.com.
I tried this solution of a similar problem: https://github.com/ContinuumIO/anaconda-issues/issues/37
I copied the .dll files from the map C:\Program Files (x86)\Python\Lib\site-packages\pywin32_system32 to C:\Program Files (x86)\Python\Lib\site-packages\win32\lib. However this did not solve the issue.
Is there a way to solve this error?
Thanks in advance!
EDIT: Removing and re-installing Python and all necessary packages did not resolve the issue.

Python coverage crashing when using unittest.mock

I have a Python library in which I am setting up some tests. As parts of the tests I am using MagicMock to mock some features like JSON load and files openning and closing. The tests work, everything works as expected.
The issue comes when I try to run the tests with coverage. Since I introduced the new tests that use mock I get coverage crashing.
This is the part where I am implementing the mocking:
def setUp(self):
# Setup the content of the config files for the tests
json.load = MagicMock(side_effect=file_content)
# Opening a file returns the name of the file
def get_mock_context(filename):
mock_context = MagicMock()
mock_context.__enter__.return_value = filename
mock_context.__exit__.return_value = False
return mock_context
builtins.open = MagicMock(side_effect=get_mock_context)
As mentioned, when I run the tests everything seems to be working fine
python -m unittest discover -s tests -p "*Tests.py"
...........................
----------------------------------------------------------------------
Ran 27 tests in 1.322s
OK
Nonetheless, as soon as I introduce coverage:
coverage run -m unittest discover -s tests -p "*Tests.py"
...........................
----------------------------------------------------------------------
Ran 27 tests in 1.759s
OK
Traceback (most recent call last):
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 627, in do_run
self.run_python_module(args[0], args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 122, in run_python_module
run_python_file(pathname, args, package=packagename, modulename=modulename, path0=path0)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 192, in run_python_file
exec(code, main_mod.__dict__)
File "/anaconda3/envs/c360/lib/python3.7/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 101, in __init__
self.runTests()
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 273, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: False
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/envs/c360/bin/coverage", line 10, in <module>
sys.exit(main())
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 756, in main
status = CoverageScript().command_line(argv)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 491, in command_line
return self.do_run(options, args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 641, in do_run
self.coverage.save()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 782, in save
self.data_files.write(self.data, suffix=self.data_suffix)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/data.py", line 680, in write
data.write_file(filename)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/data.py", line 467, in write_file
with open(filename, 'w') as fdata:
File "/anaconda3/envs/c360/lib/python3.7/unittest/mock.py", line 951, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/anaconda3/envs/c360/lib/python3.7/unittest/mock.py", line 1017, in _mock_call
result = effect(*args, **kwargs)
TypeError: get_mock_context() takes 1 positional argument but 2 were given
I have no idea what the issue might be. I have tried excluding the lines in the tests files that include mocking with pragma: no cover but that did nothing. I cannot find a way to make this work again. Any ideas or something you think I might have missed?
EDIT:
I just cleaned a bit the code y pasted before. If I run coverage specifying that I want to to use as source only the files in my local directory (to avoid using coverage in python libraries etc) I get the following error. Still not working, but a different one:
Traceback (most recent call last):
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 627, in do_run
self.run_python_module(args[0], args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 122, in run_python_module
run_python_file(pathname, args, package=packagename, modulename=modulename, path0=path0)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 192, in run_python_file
exec(code, main_mod.__dict__)
File "/anaconda3/envs/c360/lib/python3.7/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 101, in __init__
self.runTests()
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 273, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: False
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/envs/c360/bin/coverage", line 10, in <module>
sys.exit(main())
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 756, in main
status = CoverageScript().command_line(argv)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 491, in command_line
return self.do_run(options, args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 641, in do_run
self.coverage.save()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 781, in save
self.get_data()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 834, in get_data
self._post_save_work()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 864, in _post_save_work
self._find_unexecuted_files(src)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 913, in _find_unexecuted_files
for file_path, plugin_name in itertools.chain(py_files, plugin_files):
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 910, in <genexpr>
py_files = ((py_file, None) for py_file in find_python_files(src_dir))
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/files.py", line 416, in find_python_files
del dirnames[:]
TypeError: 'tuple' object does not support item deletion

Conda: Changing Python Version. Unusual traceback

Tried to change python version from 3.6 to 3.4 to use with tensorflow. I used the conda package manager. I typed
conda install python=3.4.0
When I tried to utilize ipython I got this weird traceback as I was typing my first import statement:
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\interface.py", line 394, in run
self.eventloop.run(self.input, self.create_eventloop_callbacks())
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\eventloop\win32.py", line 87, in run
self._process_queued_calls_from_executor()
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\eventloop\win32.py", line 147, in _process_queued_calls_from_executor
c()
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\interface.py", line 326, in redraw
self._redraw()
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\interface.py", line 351, in _redraw
self.renderer.render(self, self.layout, is_done=self.is_done)
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\renderer.py", line 442, in render
previous_width=(self._last_size.columns if self._last_size else 0))
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\renderer.py", line 153, in _output_screen_diff
current_pos = move_cursor(Point(y=y, x=c))
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\renderer.py", line 90, in move_cursor
_output_cursor_backward(current_x - new.x)
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 256, in cursor_backward
self.cursor_forward(-amount)
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 249, in cursor_forward
sr = self.get_win32_screen_buffer_info().dwCursorPosition
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 150, in get_win32_screen_buffer_info
raise NoConsoleScreenBufferError
prompt_toolkit.terminal.win32_output.NoConsoleScreenBufferError: No Windows console found. Are you running cmd.exe?
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Anaconda3\Scripts\ipython-script.py", line 5, in <module>
sys.exit(IPython.start_ipython())
File "C:\Program Files\Anaconda3\lib\site-packages\IPython\__init__.py", line 119, in start_ipython
return launch_new_instance(argv=argv, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "C:\Program Files\Anaconda3\lib\site-packages\IPython\terminal\ipapp.py", line 348, in start
self.shell.mainloop()
File "C:\Program Files\Anaconda3\lib\site-packages\IPython\terminal\interactiveshell.py", line 440, in mainloop
self.interact()
File "C:\Program Files\Anaconda3\lib\site-packages\IPython\terminal\interactiveshell.py", line 423, in interact
code = self.prompt_for_code()
File "C:\Program Files\Anaconda3\lib\site-packages\IPython\terminal\interactiveshell.py", line 333, in prompt_for_code
pre_run=self.pre_prompt, reset_current_buffer=True)
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\interface.py", line 404, in run
self._redraw()
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\interface.py", line 351, in _redraw
self.renderer.render(self, self.layout, is_done=self.is_done)
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\renderer.py", line 442, in render
previous_width=(self._last_size.columns if self._last_size else 0))
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\renderer.py", line 119, in _output_screen_diff
current_pos = move_cursor(Point(0, 0))
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\renderer.py", line 90, in move_cursor
_output_cursor_backward(current_x - new.x)
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 256, in cursor_backward
self.cursor_forward(-amount)
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 249, in cursor_forward
sr = self.get_win32_screen_buffer_info().dwCursorPosition
File "C:\Program Files\Anaconda3\lib\site-packages\prompt_toolkit\terminal\win32_output.py", line 150, in get_win32_screen_buffer_info
raise NoConsoleScreenBufferError
prompt_toolkit.terminal.win32_output.NoConsoleScreenBufferError: No Windows console found. Are you running cmd.exe?
If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev#scipy.org
You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.
Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
%config Application.verbose_crash=True
Can't make heads or tails of it. Any help is appreciated.

GAE can't generate discovery file

I upgraded my GAE SDK to Version 1.8.5 and cannot generate a discovery file anymore with endpointscfg.py gen_discovery_doc -o.-f rpc .../main.FlyFlap.APi
I get the errors below.
I tried to delete the file google_appengine/lib/cacerts/urlfetch_cacerts.txt and certs.txt like it is suggested here, but it didn't help. Does someone got same problems?
Traceback (most recent call last):
File "/usr/local/bin/endpointscfg.py", line 196, in <module>
run_file(__file__, globals())
File "/usr/local/bin/endpointscfg.py", line 192, in run_file
execfile(script_path, globals_)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/endpointscfg.py", line 472, in <module>
sys.exit(main(sys.argv))
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/endpointscfg.py", line 467, in main
args.callback(args)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/endpointscfg.py", line 348, in _GenDiscoveryDocCallback
output_path, hostname=hostname)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/endpointscfg.py", line 188, in GenDiscoveryDoc
service_configs = GenApiConfig(service_class_names, hostname=hostname)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/endpointscfg.py", line 144, in GenApiConfig
module_name, base_service_class_name = service_class_name.rsplit('.', 1)
ValueError: need more than 1 value to unpack
updated osx and it works again.

Categories

Resources