I get a nameError when using Serial. I believe that i am not importing serial properly.
from serial import *
serialPort = Serial("COM3", 9600)
i've installed pyserial through pip. Am i doing something wrong? Has anyone else experienced this error?
You called your file serial.py, which will shadow the installed serial module.
Rename your file to something else, then it should work.
Related
I am trying to make connection with my raspberry Pi and my PC windows through MQTT protocol. And I have a problem I cant solve on my PC - I can t import library that I have installed to my program:
import paho.mqtt.client as mqtt
Results in the error
ModuleNotFoundError: No module named 'paho'
The topic was already issued here (Import Error: paho.mqtt.client not found), but solution doesnt work for me.
I see it in the python folder:
C:\Users\mhucl\AppData\Local\Programs\Python\Python310\Lib\site-packages\paho\mqtt\client
I see it in the pip list. But the program can t find it. Do you know where could be the problem? I use python version 3.10.3. I tried it on different PC aswell and the same result. I followed all solutions on youtube, forums and nothing. Thank you very much.
Here are screenshots if someone would like to see:
https://uschovna.cz/zasilka/WLUWM5TNP7HP7GDG-XKP/UCKZFIZHCI
pip install paho-mqtt
https://pypi.org/project/paho-mqtt/
I solved my error by using this command
Whenever i execute the code below it gives me following Error:
ImportError: cannot import name 'Serial' from 'serial' (unknown location)
Code:
from serial import Serial
arduinodata = Serial('com4',9600)
print("Enter n to ON LED and f to OFF LED")
while 1:
input_data = raw_input()
print ("You Entered"+input_data)
if (input_data == 'n'):
arduinodata.write(b'1')
print("LED ON")
if (input_data == 'f'):
arduinodata.write(b'0')
print("LED OFF")
I have installed all the required python modules. Like pyserial, pyfirmata etc but it is continuously giving me this error.
I encountered the same problem. I first uninstalled pyserial from all pip, pip3, and conda (I had it on all 3), and then re-installed it. It then worked fine for me. Hope that helps!
I got exactly this problem, as well. It was caused by the "pyserial" and "serial" libraries installed on per-user basis with pip while pyserial was also installed system-wide (possibly in a different version, using the Linux package manager).
Solution
Removing the per-user version fixed the problem in my case.
Most likely missing an
__init__.py
file or the module, or the file sub-directory for the module (Serial) is on a different layer than the file executable. Hope that helps :).
I got same problem while try to install serrial on rpi4
In this tutorial
http://wiki.ros.org/rosserial_arduino/Tutorials/Arduino%20IDE%20Setup
if you install by:
2.1.1(RECOMMENDED) Installing Binaries on the ROS workstation
It work well
But if you install follow:
2.1.2 Installing from Source onto the ROS workstation
It said : ImportError: cannot import name 'Serial' from 'serial'
I dont know why, but when install by 2.1.1 it worked well.
Uninstalling serial, pyserial
pip uninstall serial
And
pip uninstall pyserial
Then reinstalling pySerial
pip install pySerial
Worked for me.
Note however that I am using a virtual environment
I had to rename serial.py to something else (serial0.py) in my C:/python39 folder and it fixed the problem.
I'm aware of this being the same question as in this thread. However, I tried the named solutions (yes, all of them [I think]) and it still gives the same error message.
I tried to establish a serial connection between my Raspberry and Arduino (Mega 2560). After installing the serial package (pip install serial, sudo apt-get install python-serial), my script file being called "arduino.py", the code being:
import serial
MySer = serial.Serial('/dev/ttyUSB0', 9600)
I still get the same error message (AttributeError: 'module' object has no attribute 'Serial').
Now did I do something inherently wrong? Do I have to set some permissions or... I'm a little lost here. ^^
Thanks in advance,
Tim
Thanks dda - that was the right direction.
I uninstalled serial and installed pyserial, now it works phenomenally (at least from what I gather in the few minutes between coffee and work).
Thank you so much!
Best of wishes,
Tim
This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 4 years ago.
I'm trying to access a serial port with Python 2.6 on my Raspberry Pi running Debian.
My script named serial.py tries to import pySerial:
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
ser.write("hello world!")
For some reason it refuses to establish the serial connection with this error:
AttributeError: 'module' object has no attribute 'Serial'
When I try to type the same code in the interactive Python interpreter it still doesn't work.
Strangely, it used to work about a couple hours ago.
What could be the problem? I've tried to fix this for a while, installing pySerial again, rewriting my code, double-checking the serial port, etc.
I'm adding this solution for people who make the same mistake as I did.
In most cases: rename your project file 'serial.py' and delete serial.pyc if exists, then you can do simple 'import serial' without attribute error.
Problem occurs when you import 'something' when your python file name is 'something.py'.
I accidentally installed 'serial' (sudo python -m pip install serial) instead of 'pySerial' (sudo python -m pip install pyserial), which lead to the same error.
If the previously mentioned solutions did not work for you, double check if you installed the correct library.
You're importing the module, not the class. So, you must write:
from serial import Serial
You need to install serial module correctly: pip install pyserial.
You have installed the incorrect package named 'serial'.
Run pip uninstall serial for python 2.x or pip3 uninstall serial
for python 3.x
Then install pyserial if not already installed by
running pip install pyserial for python 2.x orpip3 install pyserial for python 3.x.
If you are helpless like me, try this:
List all Sub-Modules of "Serial" (or whatever package you are having trouble with) with the method described here: List all the modules that are part of a python package
In my case, the problems solved one after the other.
...looks like a bug to me...
This error can also happen if you have circular dependencies. Check your imports and make sure you do not have any cycles.
Yes this topic is a bit old but i wanted to share the solution that worked for me for those who might need it anyway
As Ali said, try to locate your program using the following from terminal :
sudo python3
import serial
print(serial.__file__) --> Copy
CTRL+D #(to get out of python)
sudo python3-->paste/__init__.py
Activating __init__.py will say to your program "ok i'm going to use Serial from python3". My problem was that my python3 program was using Serial from python 2.7
Other solution: remove other python versions
Cao
Sources :
https://raspberrypi.stackexchange.com/questions/74742/python-serial-serial-module-not-found-error/85930#85930
Tryhard
I'm trying to write a program that uses the scapy modules. I'm using PyDev for my development but it keeps giving me errors when I import certain parts of the Scapy module. I'm pretty sure I have my import paths in PyDev set up correctly. I've looked at some of the other questions involving "Unresolved Import" errors on here. However, none of the suggestions I saw seemed to help.
The weird thing is that it is only part of the scapy modules that don't work. So for instance PyDev doesn't complain when I do
from scapy.all import Ether, sendp
However, when I do
from scapy.all import IP, UDP
I get errors.
I thought maybe I was importing the wrong modules but when I go to the interpreter and type in the second example it gives no errors and then I can create IP packets using IP(params), which is what I'm trying to do in my program.
I installed scapy using the ubuntu repositories, but when I started having import problems I downloaded the latest version from scapy.net and used the setup script. I even copied the zip and put it in my /usr/local/lib/python2.7/site-packages folder and added it to my python path in PyDev. But nothing seems to get rid of the error.
Any suggestions on what could be causing this and how to fix it?
Have you tried adding 'scapy' to the forced builtins? See: http://pydev.org/manual_101_interpreter.html for details.
I got a chance to play some more with this. I still don't know why PyDev gives me an import error when it works fine in the interpreter, however, I did find a way around it. To import things like IP, UDP, and TCP I'm now using the following
from scapy.layers.inet import IP, TCP, UDP
For non IPv4 stuff
from scapy.all import <Module Name>
seems to work just fine.