Sending E-mail Hotmail Account via Raspberry Pi - python

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.

Related

ValueError: source code string cannot contain null bytes when importing a package in python

from pyModbusTCP.client import ModbusClient
I am using pycharm and I have a class file which uses methods from pyModbusTCP package to communicate with devices over modbus. This class files exists as part of a directory in my project, where under the project folder I have a folder for device1 containing that particular class file along with some others. The idea was to simply duplicate the folder and title it device2, the intent being device2 functions as a backup to device1.
I created a device2 folder, made a main.py and copied most of the other contents from device1. The console is giving me only the error from the title at line 1, meaning the very first import statement that I linked is the cause for me receiving the error from the title. Given all that I have no idea what the error is even referring to, could anyone help me troubleshoot? Thanks!
EDIT:
Traceback (most recent call last):
File "/home/env1/Simulation/main.py", line 11, in <module>
import read_from_modbus
File "/home/env1/Simulation/read_from_modbus.py", line 1, in <module>
from pyModbusTCP.client import ModbusClient
ValueError: source code string cannot contain null bytes

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.

calling QgsDataSourceUri with QGIS for python plugin

TLDR : using QGIS, I'm trying to develop a python plugin to update a database. Unfortunately I get immediatly an error : Traceback (most recent call last):
File "", line 1, in
NameError: name 'QgsDataSourceUri' is not defined
More detailed:
I work on QGIS2.18 to develop a plugin to update some data located on a postgres database.
for this, I want to use this kind of code:
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtCore import QSettings
from PyQt4.QtCore import QSettings
from qgis.core import QgsVectorLayer, QgsDataSourceURI
uri = QgsDataSourceUri()
# set host name, port, database name, username and password
uri.setConnection(hote_IP, "5432", base_de_donnee, utilisateur, mot_de_passe)
# set database schema, table name, geometry column and optionally
# subset (WHERE clause)
#uri.setDataSource("public", "roads", "the_geom", "cityid = 2643")
uri.setDataSource("", sql, "geom", "", "gid")
vlayer = QgsVectorLayer(uri.uri(), zapm, "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
(I got the code from the net, I'll adapt it later on)
My problem : when I try to run this code on the Python console of QGIS, I immediatly get the error
Traceback (most recent call last):
File "", line 1, in
NameError: name 'QgsDataSourceUri' is not defined
even when I only run the import and the line uri = QgsDataSourceUri(), I get the same error message.
I have not been able to find out how to correct this issue.
problem of installation of QGIS? of python? bad imports?
Config:
qgis 2.18.20
python 3.6.5
If anyone has an idea on how to solve this, I would be really glad.
Thanks,
Erwann
You simply are using the wrong class name. It should be uri = QgsDataSourceURI() instead of uri = QgsDataSourceUri() because you've imported QgsDataSourceURI and not QgsDataSourceUri
QGIS and QT Python classes are case-sensitive. You can confirm the exact syntax looking at QGIS 2.18 related API.

Issue with importing and printing variable

Recently, I've tried to get comfortable with the open and write functions. I've encountered a problem which goes as follows: I use one script to write email = example#gmail.com in a file named "config.py". This is done using the following code: (All sensitive info has been replaced)
email = raw_input("What is your email? ")
f = open("config.py","w") #opens file
f.write("email = '{}'".format(email))
f.close() #This needs to be here else the file won't save
Then I try to import and open config.py in a separate script which looks like this:
import config
print email
However, I receive this error message:
Traceback (most recent call last):
File "/Users/MyUser/Desktop/untitled folder/Savetests/savetest_loading.py", line 1, in <module>
import config
File "/Users/MyUser/Desktop/untitled folder/Savetests/config.py", line 1
email = example#gmail.com
^
SyntaxError: invalid syntax
I have tried writing the file with quotations however this creates conflicts with the .format(). I am using python 2.7
My question is what should I do?
Thank you,
User
You need to quote the email:
email = 'example#gmail.com'
And in the module where import it, qualify the email with module name:
import config
print config.email
Or import email using from .. import .. statement:
from config import email
print email

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

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.

Categories

Resources