NameError when using reload() - python

I've got file named recommend.py. It has a dict data named critics.
When I try to reload it in the interpreter it gives the following error:
>>> from recommend import critics
>>> reload(recommend.py)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'recommend' is not defined
>>>
I'm new to python. Please help me.

recommend.py is parsed as recommend . py which means that python looks for an object bound to the name recommend and then tries to get the py attribute from it. That doesn't work because you don't have an object named recommend in the current namespace and because even if you did have an object bound to that name, it probably wouldn't have an attribute py.
Of course, you'll need to give reload an actual module object. Something more like:
import recommend
reload(recommend)

reload() takes a module object, not a filename:
import recommend
reload(recommend)

Related

how to modify python's built in modules

I want to add functions to python's built in module. As a example, there is no find function in list. I want to add it. There is some other functions I want to add. Can somebody please tell me how can I do that? Is their any way I can do that?
Adding functions to the builtins module is simple:
import builtins
builtins.find = my_find_function
But the built in classes are usually immutable, so they cannot be changed.
>>> list.hello = "hello"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'list'

How can I call a simplekml module object?

I'm new to programming and I can't able to run this code it often shows an error like module object is not callable. Can anyone sort this out for me?
import simplekml
kml = simplekml.kml() #what's wrong here?
kml.newpoit(name="sample",coords[(11,12)])
kml.save("H:\\python\\point.kml")`
Traceback (most recent call last):
File "H:/programs/practice.py", line 2, in <module>
kml = simplekml.kml()
TypeError: 'module' object is not callable
I guess you meant to call simplekml.Kml() (with capital K) to create an instance of this class. So probably it was just misspelling.
simplekml is a module, which you import in the very first line of your code.
I advise you to have a look at simplekml.Kml class documentation.
simplekml.kml is a module inside simplekml, and thus isn't callable, as the error message says. You probably meant to call simplekml.Kml() (with a capital K), to create a Kml instance.

How can I call built-in functions in Python using Boost.Python

I know we can import modules and just embed Python code in C++ and evaluate it. But how can I use built-in functions like print or open? These functions off course aren't module. Evaluating embedded open statement just gives me the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'open' is not defined
Stuck. Please help me.
Try importing the builtins and io module and if you want any other function just call the __module__ attribute to find about which module to import
>>> print.__module__
'builtins'
>>> open.__module__
'io'

In python after I import a module is there a way of finding what physical file it was loaded from?

Something's acting up in my math package I think and I want to ensure I'm loading the correct module. How do I check the physical file location of loaded modules in python?
Use the __file__ attribute:
>>> import numpy
>>> numpy.__file__
'/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'
Note that built-in modules written in C and statically linked to the interpreter do not have this attribute:
>>> import math
>>> math.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
An other way to obtain the path to the file is using inspect.getfile. It raises TypeError if the object passed is a built.in module, class or function.
On a side note, you should avoid using names that conflict with language built-ins or standard library modules. So, I'd suggest you to rename your math package to something else, or, if it is part of a package like mypackage.math, to avoid importing it directly and use mypackage.math instead.
Check themodule.__file__.
import urllib
print urllib.__file__
>>> import math
>>> math.__file__
'/usr/lib/python2.7/lib-dynload/math.so'

Why doesn't this Python work? Simple Oop

class UserDict:
def __init__(self, dict=None):
self.data = {}
if dict is not None: self.update(dict)
I created a file "abc.py" and put above in it.
>>> import abc
>>> d = abc.UserDict()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'UserDict'
Most certainly you are importing the Python abc module for abstract base classes instead of your own abc.py. Better choose a different name for your module.
Edit: Of course it is possible to have your own module with the same name as a built-in module and to import it. You have to make sure that your module is in the interpreter's working directory or set the Python path correctly. But it is much easier to avoid the name clash -- in particular in this case, where you presumably don't care about the module's name anyway.

Categories

Resources