#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    5
    Rep Power
    0

    newb question about modules


    alright, im using pythonwin, and im going through the tutorials on python.org, and im in modules, but i cant seem to import the module into the interactive window
    it tells me to create a fibo.py file with whatever in it and save it "in the current directory" then do "import fibo" in the interactive window. where do i need to save the file? i tried saving in different places on C:\Python24\ , and directoryies within it, but everything i try gives this

    Traceback (innermost last):
    File "<interactive input>", line 1, in ?
    ImportError: No module named fibo

    help please
    -jordan
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    try typing:

    Code:
    >>> import os
    >>> os.getcwd()
    (without the '>'s ofcourse) and save your module in the directory that the os.getcwd() function returns.

    also do this:

    Code:
    >>> import sys
    >>> sys.path
    and you will get a list of all directories that are in your path, ie all the directories that are searched for modules when you try importing something. saving it in one of those directories will work.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Or you can do

    os.chdir('c:/path/to/my/dir')

    to change the current directory to wherever your file is.

    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    I just had a play with Pythonwin and it starts you (or atleast me, anyway) in C:\Python23\lib\site-packages, but still if I make a quick script and try and import it by just saving the file in the site-packages dir it works fine, so obviously you've got your path set up a bit differently.

    just about the chdir() solution, isn't it rather ad hoc, seeing as you'd need to place it in every script rather than just saving the scripts on python's path?
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Most of us wouldn't use IDLE to write full programs, (though I'm sure some do,) rather IDLE is usually used to test bits of code and ideas and to see the results quickly and easily.

    So in this context using os.chdir() to move the the same directory would be perfectly acceptable. But, just to provide another working example. You could always use:

    >>> import sys
    >>> sys.path.append('c:/path/to/my/dir')
    >>>
    to add the modules location directly into Python search path. As rebbit suggested.

    Mark.
    Last edited by netytan; October 13th, 2004 at 12:32 PM.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    167
    Rep Power
    10
    Originally Posted by netytan
    Most of us wouldn't use IDLE to write full programs....
    Mark.
    what would you use? i just started using wxPython (PyAlaCrust)
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    On windows I've been using Code Genie for a long time. Just because it's a nice little editor, and after writing my own Python syntax highlighting and configuring it to run Python from the editor with a one click it just feels good.

    Since Switching to the Mac I've been useing Xcode, their isn't a massive amount of Python support but it is a very well designed, free IDE, which more than makes up for any short comings.

    In either case, if I want access to the Python shell test something I either fire up the command line and use the Python shell directly. Or just use good old IDLE or PyCrust.

    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    5
    Rep Power
    0

    Thumbs up sweet


    thanks guys, turns out that pythonwin was trying to import from
    C:\program files\python\pythonwin\
    not the regular python directory, so it works now!

    thanks abunch, and its good to see that there is a lot of activity here so i can return next time im stuck

    -jordan

IMN logo majestic logo threadwatch logo seochat tools logo