I am trying to set up a set of scripts I got from github. I have a utils.py file that has the bellow python code:
p_dir = os.path.dirname(os.path.realpath(__file__)) + "/"
username = getpass.getuser()
myData = parse_config(p_dir+"../config/data.config")
myPars = parse_config(p_dir+"../config/parameters.config")
myPipelines = parse_config(p_dir+"../config/pipeline.config")
When I run the script I get the bellow error:
myData = parse_config(p_dir+"../config/data.config")
NameError: name 'parse_config' is not defined
I thought that the required module has not been imported but it seems that thats not the case as I get the same error when I import configparser. Any idea on which module the parse_config()might belong or in general on how to fix the error?
Thanks in advance :) !
Related
I was learning about accessing data through url in python ....And I have written following code
import json
import urllib.request as ur
url= input('Enter site: ')
print( 'Retrieving ', url)
data = ur.urlopen(url).read()
info = json.loads(data)
tot = 0
print ('Retrieved ', len(data), 'characters')
print ('Count: ', len(info['comments']))
for i in range(0, len(info['comments'])):
tot += int(info['comments'][i]['count'])
print ('Sum ', tot)
And I was getting the following error
data = ur.urlopen(url).read()
AttributeError: partially initialized module 'urllib.request' has no attribute '
urlopen' (most likely due to a circular import)
Can anyone help me?? Thank you in advance !!
The only cause of this error is if you have a file that is named the same as the module which leads to a circular import. It also doesn't have to be the same script, it can also be any other files in the same folder.
Check and rename your files to ensure that they don't contain the module names such as requests.py, urllib.py, or any other relevancies. Fix this and it should solve your problem.
I faced the same issue earlier & here is the fix.
I used the name urlilib as my file name:
I changed my file name to some other words & it WORKED FOR ME! :)
Hope you find the answer to be simple & useful
I am trying to use the functions __import__(...) and getattr(...) to make an object of a class that is located inside a module. That module itself is included inside a package.
The class is named TestScript. It is located in a file named TestScript.py. The file is inside a folder named TestScripts. So from my understanding of python, the file TestScript.py is interpreted as module and the folder TestScripts is interpreted as package.
So, I tried it like this:
moduleName = 'TestScripts.TestScript'
className = 'TestScript'
module = __import__(moduleName)
targetClass = getattr(module, className)
instance = targetClass()
But an AttributeError occurs with the message "module 'TestScripts' has no attribute 'TestScript'".
I expected instance to be an object of the TestScript class after the execution of this code snippet. What do I miss?
For the full explanation see __import__
To make your code do what you want:
moduleName = 'Testscripts.TestScript'
className = 'TestClass'
module = __import__(moduleName) #could use fromlist here as #Iguananaut suggests
targetModule = getattr(module, "TestScript")
targetClass = getattr(targetModule, className)
instance = targetClass()
As a note - python docs strongly discourage direct use of __import__
When I try to do a simple query using wolfram alpha I am getting these errors.
This is my code:
import wolframalpha
input = raw_input("Question: ")
app_id = "**************"
client = wolframalpha.Client(app_id)
res = client.query(input)
answer = next(res.results).text
print answer
The error is :
Can you help me figure this one?
I don't think that error output actually corresponds to the code that is posted because the error message refers to a method called 'Client' (capital 'C') and the code refers to a method 'client'.
The code is almost correct. Just change the lower-case 'c' in client.
import wolframalpha
input = input("Question: ")
app_id = "8UHTA8-5QGXGEJ4AT"
client = wolframalpha.Client(app_id)
res = client.query(input)
answer = next(res.results).text
print (answer)
Output:
Question: 9+5
14
The two other changes you will note in my code are there because I'm using Python 3.
I uninstalled the wolframalpha 3.0.1 version by using pip uninstall wolframalpha in command prompt and then installed an earlier version by using pip install wolframalpha==1.0.2 in the command prompt and all the errors were solved.
Your code is correct.
You are getting this error:
"Wolfram alpha: AttributeError: 'module' object has no attribute 'Client'"
because i think it is importing a file named wolframalpha i.e. in same directory you have another file with name wolframalpha(or most probably you have named this code as wolframalpha.py).Change the name to wolframalpha.py to wolframalpha1.py
Hope this will solve your error.
I'm in the process of installing a piece of software from Github: https://github.com/bravecollective/core
It uses MongoDB, Python and WebCore to run. I've managed to get it running and now I've arrived at the part where I need to make myself an admin user. According to the readme, the following needs to be executed in the Paster shell.
from brave.core.account.model import User
from brave.core.character.model import EVECharacter
from brave.core.permission.model import Permission, WildcardPermission
u = User.objects(username=USERNAME_HERE)[0]
u.admin = True
c = u.primary
p1 = Permission.objects(id='core.*').first()
c.personal_permissions.append(p1)
c.save()
u.save()
The username in this case being 'TigerXtrm'. However, when I do this it comes back with the following:
Welcome to the WebCore shell.
from brave.core.account.model import User
from brave.core.character.model import EVECharacter
from brave.core.permission.model import Permission, WildcardPermission
u = User.objects(username=TigerXtrm)[0]
c = u.primary
p1 = Permission.objects(id='core.*').first()
c.personal_permissions.append(p1)
Traceback (most recent call last):
File "console", line 1, in module
NameError: name 'TigerXtrm' is not defined
So NameError: name 'TigerXtrm' is not defined is what creates a problem for me. The user is created and has been entered into the MongoDB database, I've also tried lowercase and e-mail adress, both to no avail. I can't figure out why it's telling me it's not defined. Am I executing it in the wrong place or is there something wrong with the code? Or something else entirely?
That's the Python interpreter complaining because you need to quote TigerXtrm:
u = User.objects(username="TigerXtrm")[0]
I installed everything as it says on the FlickrAPI homepage but when I try to run:
import flickrapi
api_key = '1a4c975fa83048436a2086bcab7d2290'
api_password = '5e069eae20e60297'
flickrclient = flickrapi.FlickAPI(api_key, api_password)
favourites = flickrClient.favorites_getPublicList(user_id='userid')
photos = flickr.photos_search(user_id='73509078#N00', per_page='10')
sets = flickr.photosets_getList(user_id='73509078#N00')
for photo in favourites.photos[0].photo:
print photo['title']
I get this message from the command prompt:
C:\Users\Desktop>python api.py
Traceback (most recent call last):
File "api.py", line 4, in <module>
flickrclient = flickrapi.FlickAPI(api_key, api_password)
AttributeError: 'module' object has no attribute 'FlickAPI'
Any ideas?? I have tried almost everything
FlickAPI is not the same as FlickrAPI. You're missing an r.
The file C:\Users\XXXXXX\Desktop\FLICKR API\flickrapi.py is not part of the flickrapi package. Please rename it, it is masking the real library. Right now it is being imported instead of the installed package.
The flickrapi package itself consists of a directory with a __init__.py file inside of it. Printing flickrapi.__file__ should result in a path ending in flickrapi\__init__.py.
In your "flickrclient = flickrapi.FlickAPI" line, you're missing an 'r' in FlickAPI.
Also, on the next line, your *"user_id='userid'"* argument needs an actual user ID, such as '999999#N99'
Hopefully you found that & got this working a few months ago! :)