1

I am using python 2.7 by pythonwin.

I have created my own custom modules like this:

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

The file name is fiboo.py

Now i want to open pythonwin and import it. where should I put the fiboo.py?

my script path is:

D:\fiboo.py

2 Answers 2

4

You should put the module file inside PYTHONPATH

Even better, you could create your own package. For example a package named mytests.

Just create a folder in your $HOME named py_packages (for example), a mytests folder inside it (for the package), and inside it create an empty __init__.py file and your module file.

Then just add your $HOME/py_packages folder to PYTHONPATH environment variable and you'll be able to import it as:

from mytests import fiboo
Sign up to request clarification or add additional context in comments.

10 Comments

i have no Pythonpath in my system variables. what should I do?
just create the variable
what is the value of it? Also, what is $home
Actually $HOME was for *ix. If you prefer to do so you can create the py_packages (or any other name) in D:, and then set PYTHONPATH to D:/py_packages for example.
now i am able to do this from MyTests import Functions. However, I can't call the function function1() why?
|
1

First things first: if you've defined things in fiboo.py, then your module name is fiboo. You can put the file anywhere you want, so long as it's on your PYTHONPATH (or in the current working directory). Then you import it by typing import fiboo.

I highly recommend reading up on the module documentation, which by the looks of it you already are, since those functions come straight from the first module example. As the example there explains: put the file in the current directory, open a command prompt in that directory, and import it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.