AttributeError: module 'serial' has no attribute 'Serial'- Python3.8 - python-3.x

This is my first project with Python, which I wrote online by Repl.it, (disclaimer: I have very little programming experience), it's supposed to turn on an LED when I receive a message, but this error appears when I run it (I am running it on Windows 10, 64 bits).
import imaplib
import getpass
import email
import time
import serial
import os
os.system('cls')
**ser = serial.Serial("COM3", 9600)**
ser.close()
ser.open()
It says that in the bolded line, 'serial' has no attribute 'Serial', what can I do in order to fix it?
Thanks in advance and sorry for any dumb mistake I may have committed.

Repl.it for whatever reason interprets import serial as the package 'serial', not 'pyserial', even if you manually specify pyserial as a requirement. This is just an idiosyncrasy with the repl.it virtual machine as far as I can tell. Run it on your local machine with pyserial installed and it should work fine.

Related

Can not use MICE from fancyimputer (Python)

I am just trying to use the MICE function from fancyimpute.
Simple line of code from fancyimpute import MICE gives an error cannot import name 'MICE'
I did try to consult https://github.com/iskandr/fancyimpute (i find it strange that MICE is nowhere to be found there but then people are implementing it https://medium.com/logicai/5-useful-python-packages-from-kaggles-kernels-you-didn-t-know-existed-part-2-4b35ba2d812)
as well as similiar problems on stack concerning MICE and importing problems but without of luck
The below steps worked for me on MAC OSX.
Use easy_install fancyimpute in your terminal instead of pip install fancyimpute
Unable to install fancyimpute for use in Jupyter
Use 'from fancyimpute import IterativeImputer as MICE' in your jupyter notebook. Looks like MICE is now called IterativeImputer.
https://github.com/iskandr/fancyimpute/issues/81
Use 'df1 = MICE().fit_transform(df)' on your dataframe. Looks like IterativeImputer does not have the function/method 'complete' anymore and the fit or fit_transform should be used with it instead.
from fancyimpute import IterativeImputer as MICE
MICE().fit_transform(df)

Rodeo giving error on Excel import working in Spyder

Full disclosure: I am a total beginner when it comes to Python in particular and programming in general. So please bear with me.
Today I tried for the first time to play around some datasets on my own, outside of the sandboxed environment of online courses.
I downloaded both Anaconda and Rodeo (which somehow I feel more akin to than, say, Spyder or Jupyter).
Wrote down this code. It works in Spyder.
import numpy as np
import pandas as pd
myexcel="C:/Users/myname/folder/subfolder/file.xlsx"
xl=pd.ExcelFile(myexcel)
mydf=xl.parse(0)
print(mydf.head())
However, if I try to run the same code in Rodeo I get the following error message. Here, I am showing just a part.
----> 4 xl=pd.ExcelFile(myexcel)
ImportError: No module named 'xlrd'
I am getting that in Rodeo the script fail because it is missing the xlrd package, which admittedly after checking with help("modules") is not there. But I don't fully get the problem: if xlrd was quintessential to the correct execution of this code, then why doesn't it fail in Spyder?

seeing source code of objects in a python module

I came up with this question when I was trying to use pygame. I wrote the following line
pygame.time.
but pyCharm didn't give me a list of methods to choose. I wanted to use pygame.time.Clock() but when this happened I tried to see the source code of time but I couldn't. I was just able to see the source code of pygame module and in that, there was just the following line on 'time':
try:
import pygame.time
except (ImportError, IOError):
time = MissingModule("time", geterror(), 1)
So my question is that, what is 'time' object and where is it? is it just a compiled python file that came with pygame when I installed it? Can I see the methods inside it or is there a way to let pyCharm suggest the methods inside of it?
So my question is that, what is 'time' object and where is it?
time is not an object it is a module.
Pygame is well documented. A complete documentation of the pygame.time module can be found at pygame.time.
It is not necessary to import the module. Via pygame.time you can access all objects of the module. However, you can import all objects form the module with:
from pygame.time import *

Is there a way to programmatically clear the terminal across platforms in Python 3?

Long-time lurker, first time asker.
Is there a way to automatically clear the terminal in Python 3 regardless of what platform the app is being used in?
I've come across the following (from this answer) which utilises ANSI escape codes:
import sys
sys.stderr.write("\x1b[2J\x1b[H")
But for it to work cross-platform it requires the colorama module which appears to only work on python 2.7.
For context I'm learning Python by building a game of battleships, but after each guess I want to be able to clear the screen and re-print the board.
Any help is appreciated!
Cheers
I use a single snippet for all the platforms:
import subprocess
clear = lambda: subprocess.call('cls' if os.name=='nt' else 'clear')
clear()
Same idea but with a spoon of syntactic sugar:
import subprocess
clear = lambda: subprocess.call('cls||clear', shell=True)
clear()
I know of this method
import os
clear = lambda: os.system('cls')
clear()
I'm not sure if it works with other platforms, but it's working in windows python 3.x
import os
clear = lambda: os.system('clear')
clear()
That might work for linux and OS X, but I can't test.

IAC-protocol interface error on python 3

I would like to work with excell sheets (.xls likely per .ods conversion) via python while maintaining all of the sheet's original content. Unlike xlutils (http://www.python-excel.org/) the iac-protocol (http://pythonhosted.org/iac-protocol/index.html) seems to me to be more fit/elegant tool to maintain sheet's style,formulas,dropboxes etc. One of the steps to launch iac's server or interpreter (iacs/iaci) is to initialize the interface which consists among others of this command:
import iac.app.libreoffice.calc as localc
While import iac.app.libreoffice works fine
moving to calc level
import iac.app.libreoffice.calc
throws following error
import iac.app.libreoffice.calc
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3.4/site-packages/iac/app/libreoffice/calc.py", line 11, in
from uno import getComponentContext
ImportError: cannot import name 'getComponentContext'
From what I've learned so far on this forum it might be linked to method name duplicity between two modules. This is where I am stuck. How do I learn which other module has such name of a method and how to fix it? Both iac-protocol and unotools are modules downloaded via pip3. I did not created method of such name in any script.
Thank you in advance for any advice!
Python3.4 on Scientific Linux release 7.3 (Nitrogen) LibreOffice 5.0.6.2 00(Build:2)
Some questions to narrow down the problem:
Did you start libreoffice listening on a socket first?
Did you import anything else before import iac.app.libreoffice.calc?
What happens when you start python in a terminal and enter from uno import getComponentContext?
I installed iac-protocol on Linux Mint and was able to import iac.app.libreoffice.calc and then use it. The installation process was complex, so I wouldn't be surprised if there is some problem with how your packages were installed, or possibly it does not work on RHEL-based systems. For one thing, it required me to install gnumeric.
The Calc "Hello World" code that worked for me is as follows.
libreoffice "--accept=socket,host=localhost,port=18100;urp;StarOffice.ServiceManager" --norestore --nofirststartwizard --nologo --calc &
python3
>>> import iac.app.libreoffice.calc as localc
>>> doc = localc.Interface.current_document()
>>> sheet = doc.getSheets().getByIndex(0)
>>> cell = sheet.getCellByPosition(0,0)
>>> cell.setString("Hello, World!")
One more thought: Have you considered using straight PyUNO starting from import uno instead of a wrapper library? That would avoid dependency on some of the extra libraries which may be causing the problem. Also there is better documentation for straight PyUNO.

Resources