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

    Join Date
    May 2004
    Posts
    27
    Rep Power
    0

    Exclamation how to add User defined Module? so, i can use that in other python script.


    Hii Friends,

    I am using Python2.3. Here, I am trying to use module filelib(developed by me) in other python script. But when i am trying to import it it gives me error that
    ImportError: No module named filelib

    I also copied it in Python directory. And also in python23\lib dir. Still it is not working.

    Can anybody give me some guidence or reference link for this. ?

    Thanks in advance.

    DevHims
  2. #2
  3. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by DevHims
    Hii Friends,

    I am using Python2.3. Here, I am trying to use module filelib(developed by me) in other python script. But when i am trying to import it it gives me error that
    ImportError: No module named filelib

    I also copied it in Python directory. And also in python23\lib dir. Still it is not working.

    Can anybody give me some guidence or reference link for this. ?

    Thanks in advance.

    DevHims
    Put it in site-package - dont know if this thread is related to the recent one, but anyways.. if your answer still stands.. then like i said.. put it in site-package
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Is the module in the same directory as your other script?

    Does
    python filelib.py
    work?

    The module filename must end in .py.
    The program that imports your module will first search the current working directory and then the PYTHONPATH. On my machine:
    Code:
    >>> import sys
    >>> sys.path
    ['C:\\PYTHON23\\Lib\\idlelib', 'C:\\WINDOWS\\System32\\python23.zip', 'C:\\PYTHON23', 'C:\\Python23\\DLLs', 'C:\\Python23\\lib', 'C:\\Python23\\lib\\plat-win', 'C:\\Python23\\lib\\lib-tk', 'C:\\Python23\\lib\\site-packages']
    >>>
    What happens when you import the module manually within the Python shell? That should work just as well.

    Grim
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    27
    Rep Power
    0
    Hii Grim,

    When i tried on python shell for filelib module, it did not work. It gives me same error " No module named filelib". One more thing is, filelib is developed in older version of python. Not using python 2.3. That could be reasion. I am not sure.

    What do u think??

    DevHims
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    If it is a compiled C library then it has to be recreated for that version of python.

    If not then ...
    Is filelib a python module text file?
    Is it called filelib.py?
    Is it in the same dirctory as python or in a directory of the python path?

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

    Join Date
    May 2004
    Posts
    27
    Rep Power
    0
    Hey Grim,

    It is python text file.It is developed in python 1.2 version. I have filelib.py and also compiled filelib.pyc. I copied both files in python library and also tried with python path Directories. Still it is not working.

    What do u suggest? I do not understand why it is not finding it, even though it is in Python folder? I do not have any clue further.

    Do u have any other way to make it work?

    DevHims
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Whether you are running this in Windows or Linux:
    Open a command window (or UNIX terminal) and change directory to wherever you have the filelib.py file. In the command window type python and then type

    import filelib

    If you are in the same directory as the file then python should find it.

    (You do have permission to read the file don't you? Under Linux at least you get "No module named ..." if you don't have read permission rather than any read permission error.)

    If it is a valid file this should succeed.

    It will load into later versions of python without a problem. (I cannot guarantee the code will work though). A new pyc file Will be created for the current version of python if it is imported successfully.

    Perhaps you should tell us a bit more about your setup.

    grim
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Get rid of the .pyc file, Python wont (shouldn't) compile another version as long as the last modifide date for the .py file matches the .pyc file. So this will result in Python barfing since the bytecode isnt compatable. Thats the theory anyway

    Good luck,

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

  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    27
    Rep Power
    0
    Thanks all.

    Now i trying with python 2.3.4. Let us see. I tried all possible ways and ways which you guys told. I guess i have to dig further in. I hope it work.

    Still, welcome for input. I am trying.

    DevHims
  18. #10
  19. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    DevHims,
    If after doing all I suggest it still does not work then either my suggestions are not clear or you have a problem that is nothing to do with python.

    You should not need to make any modifications to a standard install of Python to do such a basic thing as import a .py file.

    If after installing the latest Python you still have a problem perhaps you should describe your equipment setup, the OS (Windows or Linux version), the directory structure of the relevant files - where is the filelib.py file where is python.

    Why not post the files here or email them to me if you prefer.


    Netytan,

    If all you have is the pyc file from Python 2.1 then Python 2.2/2.3 will complain about the wrong magic bit as you say.

    If the .py file is found in the same directory then the later version of Python will silently replace the .pyc file regardless of its date stamp.

    grim
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    A new pyc file is created when the original python file has been changed (i believe a timestamp is used to do this), and since both have the same timestamp python should simply use the pyc file. Or at least this is what happens in most situations

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

  22. #12
  23. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    python 2.3 cannot use a python 2.1 or 2.2 pyc file - regardless of the timestamp. The bytecode is wrong.

    You can try this yourself on a system with more than one version of python installed:

    import a file using a python 2.2 console
    exit the console and rename the .py file.

    import the same file using a python 2.3 console (it will attempt to read in the pyc file)- it won't.

    if you don't rename the file then it will work because the pyc file is re-generated.
    Last edited by Grim Archon; June 4th, 2004 at 10:05 AM.
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    LOL i know that, all i'm saying is that Python shouldnt recreate the pyc file - and an error should occure . Just get rid of the pyc file and let Python make a new one just to be sure.

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


IMN logo majestic logo threadwatch logo seochat tools logo