Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Errors
#1
my question here
I am using a python program from a link but am getting errors on simple lines of code (below). Get errors on the first, second lines i.e.
import serial #  ImportError: "No module named serial"

from pynmea import nmea #ImportError: "No module named pynmea"

So, my question is - how to import these 2 programs i.e. from a library?
I am using these in Enthought Canopy Environment.

my code here:

import serial
from pynmea import nmea
import string
import webbrowser
#####Global Variables######################################
ser = 0
#####FUNCTIONS#############################################
def scan():
#scan for available ports. return a list of tuples (num, name)
available = []
for i in range(256):
try:
s = serial.Serial(i)
available.append( (i, s.name))
s.close() # explicit close 'cause of delayed GC in java
except serial.SerialException:
pass
return available
Reply
#2
Always post the entire error report (between the "error" tags).
What version of Python are you running?
Which OS are you using?

You need to install the module before you can use it.

Depending on the Python version you might try pip install pynmea
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
(May-19-2017, 09:27 PM)sparkz_alot Wrote: Always post the entire error report (between the "error" tags).
What version of Python are you running?
Which OS are you using?

You need to install the module before you can use it.

Depending on the Python version you might try pip install pynmea

Thanks.
 Windows10
Python 2.7.9
How to import "serial"?
Error:
C:\Users\Dell\Downloads\lat_lon.py in <module>()
      2 import webbrowser
      3 import string
----> 4 import serial
      5 
      6 def to_degrees(lats, longs):
ImportError: No module named serial
Reply
#4
Have you... tried installing it? Possibly with pip install pyserial?
Reply
#5
kendias Wrote:from pynmea import nmea #ImportError: "No module named pynmea"
You had a long post here ,about installing pynmea 2-days ago.
You have now posted code with no indentation.
kendias Wrote:I am using these in Enthought Canopy Environment.
Have you changed to Enthought since last post?
Reply
#6
Thanks. Tried to install serial from pyserial as you said; installed OK. Tried with python from cmd line and no errors.
Tried to run it in Enthought Canopy and got error. I cannot run in the python cmd line as it is a program that runs continuously and hence not suitable for running from python cmd line - or maybe I am confused as to how to run the progam.

ImportErrorTraceback (most recent call last)
C:\Users\Dell\py_1.py in <module>()
1 # -*- coding: utf-8 -*-
----> 2 import serial
3 from pynmea import nmea
4 import string
5 import webbrowser
ImportError: No module named serial
Reply
#7
When you changes you most learn how to install packages with Enthought Canopy.
Installing packages into Canopy User Python from the OS command line
PySerial is a part of Enthought as Python 2.7 Packages.
Reply
#8
Thanks - serial is OK now.
Trying this program but the 2 print outputs are not displayed.

# -*- coding: utf-8 -*-

import webbrowser
import string
import serial
from pynmea2 import nmea

def to_degrees(lats, longs):
# Convert string forms from ddmm.mmmm to dd°mm'ss.ss“
  msg = pynmea2.parse("$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D")
  lat_deg = lats[0:2]
  print lat_deg
 
  lat_mins = lats[2:4]
  print (lat_mins)
  lat_secs = round(float(lats[5:])*60/10000, 2)
  lat_str = lat_deg + u'°'+ lat_mins + string.printable[68] + str(lat_secs) + string.printable[63]

  lon_deg = longs[0:3]
  lon_mins = longs[3:5]
  lon_secs = round(float(longs[6:])*60/10000, 2)
  lon_str = lon_deg + u'°'+ lon_mins + string.printable[68] + str(lon_secs) + string.printable[63]

  return [lat_str, lon_str]
  print(lat_str)
  print msg
Reply
#9
Sorry, not sure how to do it. Will repost when sure - please ignore previous post.
Reply
#10
Trying to define a serial port to get data.
def init_serial():
print "Found Ports:"
for n,s in scan():
  print "%s" % s
print " "

COMNUM = 5 #set you COM port # here
global ser #must be declared in each fxn used
ser = serial.Serial()
ser.baudrate = 4800
ser.port = COMNUM -1#starts at 0, so subtract 1def init_serial():
print "Found Ports:"
for n,s in scan():
  print "%s" % s
print " "


I get the foll. error:
%run "C:/Users/Dell/Downloads/lat_lon1.py"
C:\Users\Dell\Downloads\lat_lon1.py:26: SyntaxWarning: name 'ser' is assigned to before global declaration
 global ser #must be declared in each fxn used

ValueErrorTraceback (most recent call last)
C:\Users\Dell\Downloads\lat_lon1.py in <module>()
    27 ser = serial.Serial()
    28 ser.baudrate = 4800
---> 29 ser.port = COMNUM -1#starts at 0, so subtract 1
    30
    31 ser.timeout = 1 #you must specify a timeout (in seconds) so that the serial port doesn't hang
C:\Users\Dell\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\serial\serialutil.pyc in port(self, port)
   262         """
   263         if port is not None and not isinstance(port, basestring):
--> 264             raise ValueError('"port" must be None or a string, not {}'.format(type(port)))
   265         was_open = self.is_open
   266         if was_open:
ValueError: "port" must be None or a string, not <type 'int'>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  When does Python detect Errors? stamp1t 1 451 Oct-21-2023, 05:53 PM
Last Post: deanhystad
  Rmarkdown opened by python code - errors Rav013 0 2,096 Apr-27-2021, 03:13 PM
Last Post: Rav013
  Python Request Errors PythonNoob1998 7 3,997 Jan-07-2021, 05:18 PM
Last Post: buran
  Pip Syntax Errors in CMD: Windows 10 and Python 3.8.1 jamesphopper 2 4,465 Feb-08-2020, 07:21 PM
Last Post: jamesphopper
  Python stops without errors shahgourav 4 2,786 Feb-04-2020, 11:44 PM
Last Post: micseydel
  Can the comments produce errors in python? newbieAuggie2019 9 4,313 Nov-26-2019, 12:19 AM
Last Post: micseydel
  Running into errors when installing pip and pip3 on python 2.7 and python 3.6 tej7gandhi 1 2,853 May-05-2019, 10:37 PM
Last Post: snippsat
  Does Python sqlite3 detect connection errors zatlas1 6 3,980 Jan-18-2019, 06:02 AM
Last Post: zatlas1
  Errors in Python dttomoum 1 2,723 May-23-2018, 08:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020