School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,146 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,748 people online right now. Registration is fast and FREE... Join Now!




How to Import Module

 

How to Import Module

ZevhEr

10 Aug, 2009 - 12:07 AM
Post #1

New D.I.C Head
*

Joined: 18 Jul, 2009
Posts: 14


My Contributions
Ok i make a module with this name fibo.py
CODE

        def fib(n):
    a,b=0,1
    while b<n:
        print b,
        a,b=b,a+b

        
        def fib2(n):
    result=[]
    a,b=0,1
    while b<n:
        result.append(b)
        a,b=b,a+b
    return result


i save it to "C:\Python26"

but i can't import it

CODE

import fibo

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import fibo
  File "C:\Python26\fibo.py", line 1
    Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32
             ^
SyntaxError: invalid syntax





User is offlineProfile CardPM
+Quote Post


Nallo

RE: How To Import Module

10 Aug, 2009 - 01:41 AM
Post #2

New D.I.C Head
*

Joined: 19 Jul, 2009
Posts: 24



Thanked: 3 times
My Contributions
2 Problems:
First the def fib and def fib2 lines have the wrong identitation.
It should be:
CODE

def fib(n):
    a,b=0,1
    while b<n:
        print b,
        a,b=b,a+b

instead of:
CODE

        def fib(n):
    a,b=0,1
    while b<n:
        print b,
        a,b=b,a+b

But that would throw an IdentitationError, not a SyntaxError.

And the second Problem: From the Error Message you got I deduce, that the first line of your fibo.py is:
CODE

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32

Obviously that is not python code, but the python interpreter expects python code here and tries its best to understand this line as python code (which then produces a SyntaxError). Comment out this line (put a # in front of it) and try again.
User is offlineProfile CardPM
+Quote Post

ericode

RE: How To Import Module

14 Sep, 2009 - 06:27 PM
Post #3

D.I.C Head
Group Icon

Joined: 9 Dec, 2006
Posts: 104


Dream Kudos: 75
My Contributions
I just wanted to add that you may need to set the PYTHONPATH environmental variable if you are putting your module in a different directory than your main program. I had some trouble with this a while ago and found that I needed to set the variable to the directory that I had my module in, then in my main program add a line like this to import my function:

from FileNameMinusDotPYFileExtension import FunctionName
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:49PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month