Any way to see where python is importing a module from? - python

So I installed the facebook module, realized it was the wrong one, used pip to uninstall and then installed facebook-sdk. Here is my code:
import facebook
token = '[token]'
graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")
friend_list = [friend['name'] for friend in friends['data']]
print friend_list
and get
Traceback (most recent call last):
File "C:\Users\mgraves\Desktop\facebook.py", line 1, in <module>
import facebook
File "C:\Users\mgraves\Desktop\facebook.py", line 5, in <module>
graph = facebook.GraphAPI(token)
AttributeError: 'module' object has no attribute 'GraphAPI'
When looking this up, EVERY result says to uninstall facebook and facebook-sdk and reinstall facebook-sdk. And I have, many many times. I searched /python27/ for facebook afterwards to make sure the files were gone.
Is there any way on a windows machine to trace back where I am importing "facebook" from?

Module objects have a __file__ attribute, and the object representation also includes the file:
print facebook
print facebook.__file__
In your case, you are importing your own script; you named it facebook as well and are masking the installed module:
File "C:\Users\mgraves\Desktop\facebook.py", line 1, in <module>
import facebook
File "C:\Users\mgraves\Desktop\facebook.py", line 5, in <module>
graph = facebook.GraphAPI(token)
Note the filename in the first line, then the fact that the same file is used for that import. Python stores the main script as __main__, so importing the script itself results in another module being created for the actual filename.

Related

I pip installed the arcgis library, but the reverse geocoding function is not working

from geopy.geocoders import ArcGIS
from arcgis.geocoding import reverse_geocode
arcgis = ArcGIS()
results = reverse_geocode(location={"x": 103.876722, "y": 1.3330736})
print(results)
When I run this script in python, I get the following error.
Traceback (most recent call last):
File "C:\Users\18102\RentScraping\newMarketRequests.py", line 22, in <module>
results = reverse_geocode(location={"x": 103.876722, "y": 1.3330736})
File "C:\Users\18102\AppData\Roaming\Python\Python310\site-packages\arcgis\geocoding\_functions.py", line 1457, in reverse_geocode
geocoder = arcgis.env.active_gis._tools.geocoders[0]
AttributeError: 'NoneType' object has no attribute '_tools'
I pip installed the arcgis library, which I have used before for geocoding on a different project, but the reverse geocoding isn't working. I saw some documentation that had a few variations on how to provide the location parameter. I also tried reverse_geocode([103.876722, 1.3330736]) and got the same result. I think there might be something that got updated and the documentation I'm referencing is out of date. Any advice?

ImportError: cannot import name 'loads' from 'json' (unknown location)

Previos title was:
AttributeError: module 'json' has no attribute 'loads'
I changed it because it looks similar to this but at the link that i provided, the problem seems that the person was having a file called json.py that was tricking the import to think that is importing a local file and not the json from standard library. My problem is that I don't have any local file called json.py;
I am wondering if it has to do anything related to PATH or the structure of my project. Any suggestion might help.
error traceback:
File "D:\Me\IdeaProjects\src\app\repositories\user_repository.py", line 14, in get_user
user = json.loads(file.read())
I am running the code in Windows 10, and intelliJ ide.
Python version: 3.7.4
Tried the code from the official documentation this:
import json
def as_complex(dct):
if '__complex__' in dct:
return complex(dct['real'], dct['imag'])
return dct
json.loads('{"__complex__": true, "real": 1, "imag": 2}', object_hook=as_complex)
And got this error as well:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
json.loads('{"__complex__": true, "real": 1, "imag": 2}',object_hook=as_complex)
AttributeError: module 'json' has no attribute 'loads'
When I try to import loads explicitly i get this error:
ImportError: cannot import name 'loads' from 'json' (unknown location)
I had python installed in Admin account in window 10 and it was installed with Admin privileges, but when i used in another account I could not use the packages, however installing the python in the current account did fix the problem.
Try to import loads explicitly:
import json
from json import loads

ModuleNotFoundError: No module named '_beatbox'

I am trying to use python to connect with SF.
Saw some articles that show how to use it with beatbox library and I did install it.
However when trying to run simple code I'm getting error below.
Traceback (most recent call last):
File "c:/Users/user/hello/.vscode/hello.py", line 16, in <module>
import beatbox
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\beatbox\__init__.py", line 1, in <module>
from _beatbox import _tPartnerNS, _tSObjectNS, _tSoapNS, SoapFaultError, SessionTimeoutError
ModuleNotFoundError: No module named '_beatbox'
I navigate to the folder where the beatbox is installed and I see there the file _beatbox.py.
I think the file __init__.py try to import _beatbox but cannot find it for some reason.
Any idea how to solve it? What I'm missing?
Code:
import beatbox
sf_username = "xxxxxx"
sf_password = "xxxxxx"
sf_token = "xxxxxx"
def getAccount():
sf = beatbox._tPartnerNS
sf_client = beatbox.PythonClient()
password = str("%s%s" % (sf_password, sf_token))
sf_client.login(sf_username, sf_password)
accQuery = "Select Id,Name From Account limit 5"
queryResult = sf_client.query(accQuery)
print ("query result: " + str(queryResult[sf.size]))
for rec in queryResult[sf.records:]:
print str(rec[2]) + " : " + str(rec[3])
return
Can close the case. I first found that in python 3+ should use beatbox3. But then found additional errors (possible compatibility issues).
Since I notice it taking me too long, instead I tried using library simple-salesforce 0.74.2 to connect, and it worked perfect.
Probably, Python does not know where to search for the module. By default, only the sitepackages directory and your working directory is searched. You can resove this by placing a symbolic link to the beatbox package or moving it to the sitepackages directory
If you change the sitepackage folder name from "beatbox" to "_beatbox" this will solve your problem. You can then import it as: "import beatbox" and it will load in Python.

Sending E-mail Hotmail Account via Raspberry Pi

I just write this code in Python under Raspbian OS:
import smtplib
from = '****#hotmail.de'
to = '****#hotmail.de'
msg = 'Testmail'
usr = '****#hotmail.de'
psw = '****'
server = smtplib.SMTP('smtp.live.de',25)
server.login (usr,psw)
server.sendmail (from, to, msg)
server.quit()
And get following Error-Message:
Traceback (most recent call last):
File "ail.py", line 1, in <module>
import smtplib
File "/usr/lib/python2.7/smtplib.py", line 46, in <module>
import email.utils
File "/home/pi/email.py", line 6, in <module>
smtp =smtplib.SMTP('smtp.live.com',25)
AttributeError: 'module' object has no attribute 'SMTP'
What is my fault? Could somebody help me - please?
Regards
Your problem is that you named your script email.py, or maybe an earlier version of it. That means that it shadows the standard-library email module/package. So, when smtplib tries to import email or import email.utils, it gets your code instead of the stdlib code it wants.
The solution is to rename your script to not match any of the stdlib modules and packages (or at least not any of the ones you're directly or indirectly using).
If you've already renamed it to ail.py (as the traceback seems to suggest) and it's still causing problems, make sure to delete your original email.py, and any .pyc/.pyo files of the same name. As long as they're in the current working directory (or elsewhere on sys.path), they can still interfere with the stdlib.

Import Error using cPickle in Python

I am using Pickle in Python2.7. I am getting error while using cPickle.load() method. The code and error is shown below. Can someone guide me through this?
Code:
#! usr/bin/python
import cPickle
fo = open('result','rb')
dict1 = cPickle.load(fo)
Error:
Traceback (most recent call last):
File "C:\Python27\test.py", line 7, in <module>
dicts = cPickle.load(fo)
ImportError: No module named options
It seems like you can not do
import options
but when you or someone else did
cpickle.dump(xxx, open('result', 'rb'))
there was an object with a class or function of a module options that existed at this point in time, in xxx.
Solution
You can open the file binarily and replace options with the module you replaced the old module options with.
You probably created the file in your package like in module package.main by executing the file main.py or something like it, having a module options in the same directory.
Now you do import package.main, try to read the file and options is now called package.options and the module options can not be found.
How did you create this file? How do you load it now? cPickle/pickle does not transfer source code - so if you use a function you need the module when you load it.

Categories

Resources