zsierzega
Posts: 13
Joined: Fri Aug 14, 2015 5:48 am

Python import network error

Fri Aug 21, 2015 1:15 am

Hello, I am relatively new to Raspberry Pi and I was trying to set up a simple python chat between 2 raspberry pis using the code provided by the raspberry pi teaching website. This is the code for the chat. Also, here is a link to the specific page I am viewing. https://www.raspberrypi.org/learning/ne ... worksheet/

# A simple internet-chat application

import network
import sys

def heard(phrase):
print("them:" + phrase)

if (len(sys.argv) >= 2):
network.call(sys.argv[1], whenHearCall=heard)
else:
network.wait(whenHearCall=heard)

while network.isConnected():
#phrase = raw_input() #python2
phrase = input() # python3
print("me:" + phrase)
network.say(phrase)

However, whenever I try to run the code on either Pi, i get the error:
Traceback (most recent call last):
File "chat.py", line 3, in <module>
import network
ImportError: No module named network

Can someone please help? Thanks

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python import network error

Fri Aug 21, 2015 5:53 am

RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

betruk
Posts: 36
Joined: Fri Apr 24, 2015 8:17 am

Re: Python import network error

Fri Aug 21, 2015 5:58 am

I did a google search for Python import network error, and find this page about how to debug Python import failure. Should be working.
http://stackoverflow.com/questions/6134 ... rt-failure

User avatar
B.Goode
Posts: 9278
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python import network error

Fri Aug 21, 2015 7:01 am

You don't need a Google search of the obscure corners of the Internet: just read the information the Raspberry Pi Foundation have supplied for this project. https://www.raspberrypi.org/learning/ne ... uirements/

This contains the link to the software that @elParaguayo has already referred to.

zsierzega
Posts: 13
Joined: Fri Aug 14, 2015 5:48 am

Re: Python import network error

Sat Aug 22, 2015 3:12 am

Attempting to obtain the network.py item results in a 404 error. Can anyone help this?

zsierzega
Posts: 13
Joined: Fri Aug 14, 2015 5:48 am

Re: Python import network error

Sat Aug 22, 2015 3:28 am

I did everything this link instructed me to and copy and pasted the code on the 2nd attempt. No luck
https://www.raspberrypi.org/learning/ne ... /software/

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python import network error

Sat Aug 22, 2015 6:06 am

Yes. Looks like that link is broken. I'll flag it on github too.

Edit: looks like the learning documentation isn't on github...
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
B.Goode
Posts: 9278
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python import network error

Sat Aug 22, 2015 6:27 am

Issue flagged up for Raspberry Pi documentation team: https://github.com/raspberrypilearning/ ... /issues/22

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python import network error

Sat Aug 22, 2015 7:24 am

Thanks. Had just realised I had been searching on the wrong github account...
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python import network error

Sat Aug 22, 2015 11:55 am

The new link is available at the page I originally linked.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

DIYKiwi
Posts: 18
Joined: Wed Dec 19, 2012 10:33 pm

Re: Python import network error

Thu Jan 19, 2017 9:21 am

Hi. I have spent a whole day trying to follow the Networking Lessons on the raspberrypi.org website. I am a secondary teacher and want to offer this task to my computer science students. Others may have also discovered that there are some crucial errors in the networking lessons, particularly in the chat.py code. It took me ages and much internet searching and trial and error to get my two raspi computers to chat to each other. Here is the correct chat.py code:

Code: Select all

import network
import sys

def heard(phrase):
  print "them:" + phrase

print "Chat Program"

if (len(sys.argv) >= 2):
  network.call(sys.argv[1], whenHearCall=heard)
else:  
  network.wait(whenHearCall=heard)

print "Chat away!"  
while network.isConnected():
  phrase = raw_input()
  print "me:" + phrase
  network.say(phrase)
You'll notice a few differences with the notes in the Raspberry Pi Networking Lessons.

The other thing to note is that you need to edit the etc/network/interfaces file (you can use sudo nano /etc/network/interfaces as a way to edit this file). You need to add the following code to this file:

Code: Select all

iface eth0 inet static
address 192.168.0.2
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
The address in line 2 is each raspi's unique ip address. You need to do this for both of the raspi computers that you are networking, and they will of course have different addresses. You will need to reboot your pi for these changes to take effect.

You can then use ifconfig to check the ip address is exactly as you set it.

The other thing you might get is an error message saying something like "Network unreachable". This may mean you need to configure your ip address. Try configuring the eth0 by typing in a command: $ sudo ifconfig eth0 192.168.0.? netmask 255.255.255.0
Do this for both pi’s, client and server (both will have different eth0 ip addresses of course)

So there are a few tips from my experience. I found the following website more accurate and helpful than the raspberry pi networking lessons: http://blog.whaleygeek.co.uk/raspberry- ... in-python/

MrODonnell
Posts: 1
Joined: Wed Oct 25, 2017 8:27 pm

Re: Python import network error

Wed Oct 25, 2017 9:11 pm

I continue to get an error from the line:
import network

I have found network.py on github, is it possible to just manually copy this file over and use some kind of import or include function and run this script. It looks like a great demonstration for my students of the most basic network function of a computer but I have been working on this for quite a while and can't figure it out. I am totally new to Python and pretty new to raspberrypi. If I do download from the github project to my raspberrypi, can I just put them all in one folder and the run the chat.py with the include statement in it?

https://github.com/vivitenna/NetworkCha ... work-chat/

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python import network error

Thu Oct 26, 2017 6:20 am

Yes, download the file and put it in the same folder as your script. It should work.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
B.Goode
Posts: 9278
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python import network error

Thu Oct 26, 2017 1:30 pm

MrODonnell wrote:
Wed Oct 25, 2017 9:11 pm
I continue to get an error from the line:
import network
The workaround that you and @elParaguayo have discussed should work, but should not be necessary.

The Raspberry Pi Foundation resource seems to be correct.

Starting here: https://www.raspberrypi.org/learning/ne ... g-lessons/

Scroll to bottom of that page. Click on [What you will need]

In the Software section of that page, follow the link "see the software installation page. "

That page explains how to install the network module using pip3.

(In common with most Raspberry Pi Foundation resources this assumes the use of Python3.)

I have just verified these steps and they worked for me.

Return to “Python”