I am new to python and just downloaded it today. I am using it to work on a web spider, so to test it out and make sure everything was working, I downloaded a sample code. Unfortunately, it does not work and gives me the error:
"AttributeError: 'MyShell' object has no attribute 'loaded' "
I am not sure if the code its self has an error or I failed to do something correctly when installing python. Is there anything you have to do when installing python like adding environmental variables, etc.? And what does that error generally mean?
Here is the sample code I used with imported spider class:
import chilkat
spider = chilkat.CkSpider()
spider.Initialize("www.chilkatsoft.com")
spider.AddUnspidered("http://www.chilkatsoft.com/")
for i in range(0,10):
success = spider.CrawlNext()
if (success == True):
print spider.lastUrl()
else:
if (spider.get_NumUnspidered() == 0):
print "No more URLs to spider"
else:
print spider.lastErrorText()
# Sleep 1 second before spidering the next URL.
spider.SleepMs(1000)
And what does that error generally
mean?
An Attribute in Python is a name belonging to an object - a method or a variable. An AttributeError means that the program tried to use an attribute of an object, but the object did not have the requested attribute.
For instance, string objects have the 'upper' attribute, which is a method that returns the uppercase version of the string. You could write a method that uses it like this:
def get_upper(my_string):
return my_string.upper()
However, note that there's nothing in that method to ensure that you have to give it a string. You could pass in a file object, or a number. Neither of those have the 'upper' attribute, and Python will raise an Attribute Error.
As for why you're seeing it in this instance, you haven't provided enough detail for us to work it out. Add the full error message to your question.
1) Put code in Try ... Except block. get exception details.
2) Could you tell StackTrace details means which line # and method thrown error
And also are you able to run other simple python scripts without any error. Means just try to run some sample script etc.
Related
I have a flask python server that runs in a docker container. I have found that once the execution reaches a certain point, the container just crashes, and no exceptions or messages are thrown even if you surround your code in any try-except block.
The problem seems to arise when the code tries to access a certain variable. In my case it crashes when accessing the bar variable:
try:
logger.trace(foo)
logger.trace(bar)
except Exception as e:
logger.error(e)
My code is this simple. Reading foo and logging its value works as expected but the bar variable is never logged and the container crashes. The console only shows this message after logging foo:
Disconnected from container.
This happens whenever I make a request from Postman and reach this part of the code.
The variable bar is supposed to be an instance of a class modeled with Swagger.
What could make Python crash when a variable is read that would not show or throw any exception?
After some research I found out that this can happen when there is an infinite recursion problem.
In my case, at some point in the code the object instance gets assigned to the wrong property resulting in this infinite recursion problem:
from swagger_server.model import ObjectModel
def do_something(bar: ObjectModel) -> ObjectModel:
<some code that modifies bar>
return bar
bar = ObjectModel(
id = 'obj1'
)
bar.id = do_something(bar)
# Here is the problem. The ID now points to the sub_object creating
# infinite recursion but it does not throw an error here
# bar = do_something(bar) # This is the correct way to use the do_something() function
# do_something(bar) # As they're objects we could use this too and will modify the original object
<some more code>
logger.trace(bar) # Accessing the data triggers the crash
I also found a way to find this kinds of errors. Swagger creates automatically a test file for the controller. If the endpoint is tested for that specific case with the test file the error is shown this way:
maximum recursion depth exceeded while calling a Python object
When I tested this case with Postman the error is not thrown, it just crashes.
To solve an exercise on Jupiter Notebook, I need to perform a unit test on a function that I called city_function
def city_function(city, country):
output = city.title() + ', ' + country.title()
return output
This function is stored in "city_functions.py". The code that performs the unit test is stored in "test_cities2.ipynb". And I tried the following code to do the unit test:
import unittest
from city_functions import city_function
class CityCountryTestCase(unittest.TestCase):
# Verify if city_function works
def test_city_country_function(self):
output = city_function('lisbon', 'portugal')
self.assertEqual(output, 'Lisbon, Portugal')
unittest.main()
And I got an AttributeError of the type: AttributeError: module 'main' has no attribute.
What can I do to solve this problem?
There is a good article, that describes your problem:
The reason is that unittest.main looks at sys.argv and first parameter is what started IPython or Jupyter, therefore the error about kernel connection file not being a valid attribute. Passing explicit list to unittest.main will prevent IPython and Jupyter look at sys.argv. Passing exit=False will prevent unittest.main to shutdown the kernell process
Your last line should be like this:
unittest.main(argv=['first-arg-is-ignored'], exit=False)
I installed first the nmap module in PyCharm but received as many others the error for my nmap.PortScanner function.
Therefore, I uninstalled nmap and installed python-nmap.
Now, I have however the problem with executing the command:
import nmap
ns = nmap.PortScanner
ns.scan('My.IP.Add.ress', '1-1024', '-v')
print(ns.scaninfo())
I get for the second line the error message:
AttributeError: 'str' object has no attribute '_nmap_path'
However, watching all examples of this, show precisely these lines of code.
Can somebody please explain what is happening?
My editor also shows for ns.scaninfo() a warning that the parameter 'self' is not filled. Again, this is not what the examples show. I am very confused by this.
As always, thank you very much for your help!
(Since posting my comment I've become convinced that it is the root cause, so posting as an answer.)
You're missing some brackets on the second line. It should read:
ns = nmap.PortScanner()
As it is, you're storing the PortScanner class in ns, as opposed to an object of that class. This means that when you call ns.scan its first parameter (which it's expecting to be self, the PortScanner object with an _nmap_path attribute) is actually a string, which doesn't have the attribute. The same reason is behind your editor's warning about the self parameter not being filled, too.
I am getting a syntax error in my code with Jython. Can anyone say what's wrong in the syntax? I am new to this language, don't have much of an idea.
Error Message :
WASX7017E: Exception received while running file "namespace.jy"; exception information: com.ibm.websphere.management.exception.InvalidAttributeValException: ADMG0012E: The attribute value for attribute integration/endpoint/account is not valid.
My Code:
import sys
nodeName =sys.argv[0]
serverName =sys.argv[1]
profilePath=sys.argv[2]
machineName=sys.argv[3]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Main program
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
createNamespaceBinding(nodeName,serverName,profilePath,machineName)
I have added call to setJVMSystemProperties(), so it will ignore '/' in the name attribute, but am still facing the problem.
It worked for after adding this property file com.ibm.websphere.management.configservice.validateNames = false and then stopping and starting the server and then try to run jython script for namespace binding
r.db('usersData').table('smsRaw').get("3413b71c-1628-47eb-83fa-6a3cccdb3e62").update({"MESSAGE": r.row('MESSAGE').setInsert({"DATE":"20160111","MESSAGE":[{"ADDR":"LONDON","date":"1468385398746"}]})})
This is what i am able to run on console.I want to convert it to python code.
Below is python code i tried r.db(dbName).table(tableName).get(id).update({'MESSAGE':r.row.get_field('MESSAGE').setInsert(Doc)}).run(con)
its throwing exception. AttributeError 'GetField' object has no attribute 'setInsert
You probably want setInsert to be set_insert.