I have MBP13'R running the latest OSX.
For some reason I can't run doctest...
Python code:
def area_tri(base, height):
"""
>>>area_tri(10, 10)
50
"""
return base / 2 * height
import doctest
doctest.testmod()
Error message:
Traceback (most recent call last):
File "/Users/name/Documents/doctest.py", line 8, in <module>
import doctest
File "/Users/name/Documents/doctest.py", line 9, in <module>
doctest.testmod()
AttributeError: 'module' object has no attribute 'testmod'
I only just started programming on my Mac and I can't fix it...
Python version is 3.3.4
Your file naming is confusing the interpreter. You've named your file "/Users/name/Documents/doctest.py" which has the same name as the doctest module.
Change the name and try again (make sure you remove the old doctest.pyc as well).
Demo:
Your code breaks as expected:
msvalkon#Lunkwill:/tmp$ python doctest.py
Traceback (most recent call last):
File "doctest.py", line 8, in <module>
import doctest
File "/tmp/doctest.py", line 9, in <module>
doctest.testmod()
AttributeError: 'module' object has no attribute 'testmod'
After renaming and removing the .pyc
msvalkon#Lunkwill:/tmp$ mv doctest.py dtest.py
msvalkon#Lunkwill:/tmp$ rm doctest.pyc
msvalkon#Lunkwill:/tmp$ python dtest.py
Traceback (most recent call last):
File "dtest.py", line 9, in <module>
doctest.testmod()
File "/usr/lib/python2.7/doctest.py", line 1885, in testmod
for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
File "/usr/lib/python2.7/doctest.py", line 900, in find
self._find(tests, obj, name, module, source_lines, globs, {})
File "/usr/lib/python2.7/doctest.py", line 954, in _find
globs, seen)
File "/usr/lib/python2.7/doctest.py", line 942, in _find
test = self._get_test(obj, name, module, globs, source_lines)
File "/usr/lib/python2.7/doctest.py", line 1026, in _get_test
filename, lineno)
File "/usr/lib/python2.7/doctest.py", line 645, in get_doctest
return DocTest(self.get_examples(string, name), globs,
File "/usr/lib/python2.7/doctest.py", line 659, in get_examples
return [x for x in self.parse(string, name)
File "/usr/lib/python2.7/doctest.py", line 621, in parse
self._parse_example(m, name, lineno)
File "/usr/lib/python2.7/doctest.py", line 679, in _parse_example
self._check_prompt_blank(source_lines, indent, name, lineno)
File "/usr/lib/python2.7/doctest.py", line 766, in _check_prompt_blank
line[indent:indent+3], line))
ValueError: line 2 of the docstring for __main__.area_tri lacks blank after >>>: '>>>area_tri(10, 10)'
Notice that you are not following doctest rules but this is another problem.
Related
I got an error for this code:
from pathos.multiprocessing import ProcessingPool
def diePlz(im):
print('Whoopdepoop!')
def caller():
im = 1
pool = ProcessingPool()
pool.map(diePlz,[im,im,im,im])
if __name__=='__main__':
caller()
when I ran it with the cProfiler: (python3 -m cProfile testProfiler.py)
multiprocess.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/home/rohit/.local/lib/python3.6/site-packages/multiprocess/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/home/rohit/.local/lib/python3.6/site-packages/multiprocess/pool.py", line 44, in mapstar
return list(map(*args))
File "/home/rohit/.local/lib/python3.6/site-packages/pathos/helpers/mp_helper.py", line 15, in <lambda>
func = lambda args: f(*args)
File "testProfiler.py", line 3, in diePlz
print('Whoopdepoop!')
NameError: name 'print' is not defined
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.6/cProfile.py", line 160, in <module>
main()
File "/usr/lib/python3.6/cProfile.py", line 153, in main
runctx(code, globs, None, options.outfile, options.sort)
File "/usr/lib/python3.6/cProfile.py", line 20, in runctx
filename, sort)
File "/usr/lib/python3.6/profile.py", line 64, in runctx
prof.runctx(statement, globals, locals)
File "/usr/lib/python3.6/cProfile.py", line 100, in runctx
exec(cmd, globals, locals)
File "testProfiler.py", line 11, in <module>
caller()
File "testProfiler.py", line 8, in caller
pool.map(diePlz,[im,im,im,im])
File "/home/rohit/.local/lib/python3.6/site-packages/pathos/multiprocessing.py", line 137, in map
return _pool.map(star(f), zip(*args)) # chunksize
File "/home/rohit/.local/lib/python3.6/site-packages/multiprocess/pool.py", line 266, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/home/rohit/.local/lib/python3.6/site-packages/multiprocess/pool.py", line 644, in get
raise self._value
NameError: name 'print' is not defined
But when I ran it without the cProfiler:
$ python3 testProfiler.py
Whoopdepoop!
Whoopdepoop!
Whoopdepoop!
Whoopdepoop!
The code that I've provided is a minimal working example for the problem. There is a much larger code that I want to debug, but am not able to do so because cProfiler keeps raising weird errors.
In this case, the point of importance is
NameError: name 'print' is not defined
which means python3 is not able to recognize print itself. In my code, it was not able to recognize range.
So, I realize this is a long time after the original post, but I have this exact same issue.
In my case I was getting the exact same error as the original post - python builtin functions such as print() or len() resulted in errors like this:
NameError: name 'len' is not defined
I'm currently running multiprocess version 0.70.11.1 and dill version 0.3.3 (components of pathos that make process based parallelism work).
Based on what I found in an issue comment: https://github.com/uqfoundation/pathos/issues/129#issuecomment-536081859 one of the package authors recommends trying:
import dill
dill.settings['recurse'] = True
At least in my case, the above fixed the error!
I have the virtualbox 5.0.10 Python sdk in my source root, but when I run my code the program will fail:
Traceback (most recent call last):
File "C:/Users/xiao/.PyCharm50/config/scratches/scratch", line 2, in <module>
mgr=vboxapi.VirtualBoxManager()
File "C:\Users\xiao\Desktop\test\vboxapi\__init__.py", line 1018, in __init__
self.platform = PlatformMSCOM(dPlatformParams)
File "C:\Users\xiao\Desktop\test\vboxapi\__init__.py", line 513, in __init__
win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
File "C:\Python27\lib\site-packages\win32com\client\gencache.py", line 540, in EnsureDispatch
disp_class = CLSIDToClass.GetClass(str(disp_clsid))
File "C:\Python27\lib\site-packages\win32com\client\CLSIDToClass.py", line 46, in GetClass
return mapCLSIDToClass[clsid]
KeyError: '{7844AA05-B02E-4CDD-A04F-ADE4A762E6B7}'
Here is my code: (really simple, right?)
import vboxapi
mgr=vboxapi.VirtualBoxManager()
This program is buggy under Python 2.7.6, but runabble under Python 3.4.0.
Why? How can I fix that?
I am following this link to compile Chromium Browser and I successfully walked through each step but I get the following error at the 17th step: gclient sync:
mrgrj#mrgrj:~/chromium/buildhost/src$ gclient sync
Syncing projects: 4% ( 3/72) src/chrome/tools/test/reference_build/chrome_linSyncing projects: 5% ( 4/72) src/sdch/open-vcdiff Syncing projects: 100% (75/75), done.
________ running '/usr/bin/python src/build/landmines.py' in '/home/mrgrj/chromium/buildhost'
________ running '/usr/bin/python src/build/download_nacl_toolchains.py --mode nacl_core_sdk sync --extract' in '/home/mrgrj/chromium/buildhost'
Traceback (most recent call last):
File "src/build/download_nacl_toolchains.py", line 59, in <module>
sys.exit(Main(sys.argv[1:]))
File "src/build/download_nacl_toolchains.py", line 29, in Main
import package_version
File "/home/mrgrj/chromium/buildhost/src/native_client/build/package_version/package_version.py", line 40, in <module>
import argparse
ImportError: No module named argparse
Traceback (most recent call last):
File "/home/mrgrj/chromium/depot_tools/gclient.py", line 2313, in <module>
sys.exit(main(sys.argv[1:]))
File "/home/mrgrj/chromium/depot_tools/gclient.py", line 2299, in main
return dispatcher.execute(OptionParser(), argv)
File "/home/mrgrj/chromium/depot_tools/subcommand.py", line 252, in execute
return command(parser, args[1:])
File "/home/mrgrj/chromium/depot_tools/gclient.py", line 2056, in CMDsync
ret = client.RunOnDeps('update', args)
File "/home/mrgrj/chromium/depot_tools/gclient.py", line 1528, in RunOnDeps
self.RunHooksRecursively(self._options)
File "/home/mrgrj/chromium/depot_tools/gclient.py", line 983, in RunHooksRecursively
hook, cwd=self.root.root_dir, always=True)
File "/home/mrgrj/chromium/depot_tools/gclient_utils.py", line 293, in CheckCallAndFilterAndHeader
return CheckCallAndFilter(args, **kwargs)
File "/home/mrgrj/chromium/depot_tools/gclient_utils.py", line 538, in CheckCallAndFilter
rv, args, kwargs.get('cwd', None), None, None)
File "/home/mrgrj/chromium/depot_tools/subprocess2.py", line 37, in __init__
super(CalledProcessError, self).__init__(returncode, cmd, output=stdout)
TypeError: __init__() got an unexpected keyword argument 'output'
Any hints on how can I get rid of it ? I will update this post with any necesarry information that you'll need.
Update the python version to 2.7 or greater. One of the problems is that you don't have argparse which is on the newest version of python.
I am testing my Python script with funkload.
I use this command :
fl-run-bench -c 1:2 test_RecuperationPhotos.py RecuperationPhotos.test_RecPhotos
I have this error :
Traceback (most recent call last):
File "/usr/local/bin/fl-run-bench", line 8, in <module>
load_entry_point('collective.funkload==0.3', 'console_scripts', 'fl-run-bench')()
File "build/bdist.macosx-10.8-intel/egg/pkg_resources.py", line 343, in load_entry_point
File "build/bdist.macosx-10.8-intel/egg/pkg_resources.py", line 2354, in load_entry_point
File "build/bdist.macosx-10.8-intel/egg/pkg_resources.py", line 2060, in load
File "/Library/Python/2.7/site-packages/collective/funkload/bench.py", line 8, in <module>
from zope.testing.testrunner import runner
ImportError: cannot import name runner
I cannot understand what the error means.
I think you have a situation with the installation of funkload. I had the same issue last month.
Has anyone else encountered the situation where they have written unit tests for their code into a file named unittest.py, and found it to conflict with NumPy's unittest.py module? In other words, if I write this to unittest.py in a local directory:
if __name__ == "__main__":
print "pre-import"
#import numpy
print "post-import"
Then (no surprises here):
% python unittest.py
pre-import
post-import
But if I do:
if __name__ == "__main__":
print "pre-import"
import numpy
print "post-import"
I get:
% python unittest.py
pre-import
Traceback (most recent call last):
File "unittest.py", line 3, in <module>
import numpy
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/core/__init__.py", line 40, in <module>
from numpy.testing import Tester
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/testing/__init__.py", line 8, in <module>
from unittest import TestCase
ImportError: cannot import name TestCase
Subsequently, iPython fails to load:
% ipython
WARNING: IPython History requires SQLite, your history will not be saved
Traceback (most recent call last):
File "/home/jbbrown/local_bin/python/bin/ipython", line 7, in <module>
launch_new_instance()
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 402, in launch_new_instance
app.initialize()
File "<string>", line 2, in initialize
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/config/application.py", line 84, in catch_config_error
return method(app, *args, **kwargs)
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 312, in initialize
self.init_shell()
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 332, in init_shell
ipython_dir=self.ipython_dir)
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/config/configurable.py", line 318, in instance
inst = cls(*args, **kwargs)
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/interactiveshell.py", line 183, in __init__
user_module=user_module, custom_exceptions=custom_exceptions
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 478, in __init__
self.init_reload_doctest()
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 662, in init_reload_doctest
doctest_reload()
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/utils/doctestreload.py", line 72, in doctest_reload
import doctest
File "/home/jbbrown/local_bin/python/lib/python2.7/doctest.py", line 2118, in <module>
class DocTestCase(unittest.TestCase):
AttributeError: 'module' object has no attribute 'TestCase'
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:
c.Application.verbose_crash=True
Interestingly, if I move the import statement outside of the "if __name__" suite, e.g.,
print "pre-import"
import numpy
print "post-import"
if __name__ == "__main__":
pass
I get:
% python unittest.py
pre-import
pre-import
post-import
Traceback (most recent call last):
File "unittest.py", line 2, in <module>
import numpy
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/core/__init__.py", line 40, in <module>
from numpy.testing import Tester
File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/testing/__init__.py", line 8, in <module>
from unittest import TestCase
ImportError: cannot import name TestCase
Of course it is trivial to change the name of the file containing my unit tests and avoid this, but I wonder if anyone else encountered this and thought of an elegant workaround that doesn't include renaming their unit test file?
You can switch to absolute imports by default on Python versions newer than 2.7:
from __future__ import absolute_import
if __name__ == "__main__":
print "pre-import"
import numpy
print "post-import"