0

I'm currently using serial package for python and I have followed every tutorial on the internet but I have always got this error.

NameError: name 'serial' is not defined

I have already tried from serial import Serial and from serial import * . I have also uninstalled and re-installed the package. Thank you in advance.

from serial import Serial

sr = serial.Serial('COM4', 9600)
Rahul P
  • 2,493
  • 2
  • 17
  • 31
Zion Miguel
  • 83
  • 1
  • 3
  • 9

1 Answers1

0

Notice the difference between the following blocks of code.

(If these don't work, then your issue is that pyserial did not install properly, and you should tell us how you tried to install it).

This just imports the function Serial from the serial module into your namespace

from serial import Serial

sr = Serial('COM4', 9600) # Serial is imported into your namespace, 
# not serial, the module

This just imports the module serial into your namespace

import serial

sr = serial.Serial('COM4', 9600) # serial module is in your namespace

This imports all functions from the serial module into your namespace

from serial import *

sr = Serial('COM4', 9600) # the Serial function is imported into your namespace, 
# not serial, the module

This imports the serial module, and gives it an alias name

import serial as s

sr = s.Serial('COM4', 9600) 

The differences are subtle, but significant. Hope this helps.

bug_spray
  • 1,445
  • 1
  • 9
  • 23