Python 2.7.5 Modules Tutorial Problem

This is the place for queries that don't fit in any of the other categories.

Python 2.7.5 Modules Tutorial Problem

Postby stephen158 » Mon Oct 21, 2013 5:21 am

Hello. I am completely new to Python. I was following the Python.org tutorial to acquaint myself with the language on a laptop running Ubuntu 12.04. I reached section 6, Modules and attempted to perform the task described which involved copying the def fib(n) text (shown below) to a text file, saving it as 'fibo.py' and then importing it into python.

In Terminal I navigate to the folder where fibo.py resides, run python and type 'import fibo' which appears to work. I then entere fibo.fib(1000) as directed in the tutorial and get the following error message

Code: Select all
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'fib'


Without much further ado I installed Python 2.7.5 on a Windows XP laptop and carried out the above proceedure without any problem at all

Can anyone suggest why this seemingly simple operation should succeed on Windows XP but not on Ubuntu?


Code: Select all
# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b

def fib2(n): # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result
Last edited by stranac on Mon Oct 21, 2013 5:26 am, edited 1 time in total.
Reason: Locked. Added code tags.
stephen158
 
Posts: 2
Joined: Mon Oct 21, 2013 4:45 am

Re: Python 2.7.5 Modules Tutorial Problem

Postby Kebap » Mon Oct 21, 2013 11:02 am

Hello and welcome to python and the forums!

I am not sure why this is happening to you. I tested quickly on Win 7 and it works exactly like expected. Also, Ubuntu should find the file as well, if it is located in the same directory that you are when you start python..

Maybe somebody else has another idea as to what is going wrong here.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Python 2.7.5 Modules Tutorial Problem

Postby metulburr » Mon Oct 21, 2013 11:50 am

In Terminal I navigate to the folder where fibo.py resides, run python and type 'import fibo' which appears to work. I then entere fibo.fib(1000) as directed in the tutorial and get the following error message


I am really not sure as this works in ubuntu. I think you might have to describe path by path... the process on how you are getting this error in ubuntu.

This is an example running in ubuntu 12.04

Code: Select all
metulburr@ubuntu:~$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('fibo.py', 'w')
>>> text = '''def fib(n):
...         a, b = 0, 1
...         while b < n:
...             print b,
...             a, b = b, a+b
...
... '''
>>> f.write(text)
>>> f.close()
>>> import fibo
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> import os
>>> os.remove('fibo.py')
>>> os.remove('fibo.pyc')
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1415
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python 2.7.5 Modules Tutorial Problem

Postby hrs » Mon Oct 21, 2013 12:02 pm

Could it be that you have a missing or incorrect shebang on the first line? I.e.
Code: Select all
#!/usr/bin/env python
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Python 2.7.5 Modules Tutorial Problem

Postby metulburr » Mon Oct 21, 2013 12:11 pm

Could it be that you have a missing or incorrect shebang on the first line? I.e.

this applies when you run it in Unix making it executable and then running it directly: ./myscript.py, rather than just python myscript.py, either that or to specify an interpreter.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1415
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python 2.7.5 Modules Tutorial Problem

Postby stephen158 » Mon Oct 21, 2013 2:09 pm

Many thanks and my sincere apologies to those who offered their help. I am embarrassed to admit that I made a mistake when copying and pasting the 'fibo.py' text. I checked and double checked that the file was in my python folder but didn't check the content. The file was empty.

I copied the 'Fibo.py' script again and it worked in Ubuntu. A stupid mistake I know but I have learned a lot in the process of investigating the bogus problem.

Thanks again to all who helped
stephen158
 
Posts: 2
Joined: Mon Oct 21, 2013 4:45 am

Re: Python 2.7.5 Modules Tutorial Problem

Postby micseydel » Mon Oct 21, 2013 11:40 pm

stephen158, next time you make a post please include the entirety of your terminal session. Also, always always include the full traceback which includes the line number as well as suspect line and not just the error message and a little bit more around it, since it provides nearly everything we need to know >90% of the time. That said, kudos on using code tags in your first post and thank you for posting your solution in spite of the embarrassment (no one around here is going to judge you for that, by the way :) ).

To everyone else: he reported that he had successfully imported the file, and that the problem was that the function he wanted was not inside of it. From that, you can deduce that it isn't an operating system issue, a path issue, a shebang issue, or anything like that. We even knew with relative certainty that he even successfully imported something since we got that much of a traceback. I appreciate everything trying to figure it out, and of course thank you for that, but just realize a successful import rules out a lot ;)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1260
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: buran and 4 guests