The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
how to add User defined Module? so, i can use that in other python script.
Discuss how to add User defined Module? so, i can use that in other python script. in the Python Programming forum on Dev Shed. how to add User defined Module? so, i can use that in other python script. Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 2nd, 2004, 02:38 PM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 27
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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 
|

June 2nd, 2004, 10:25 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
Quote: | 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
|

June 3rd, 2004, 03:26 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Is the module in the same directory as your other script?
Does
python filelib.py
work?
Quote: | 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 
|

June 3rd, 2004, 09:41 AM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 27
Time spent in forums: < 1 sec
Reputation 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
|

June 3rd, 2004, 10:10 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
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 
|

June 3rd, 2004, 11:43 AM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 27
Time spent in forums: < 1 sec
Reputation 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
|

June 3rd, 2004, 12:11 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
|
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
|

June 3rd, 2004, 12:25 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

June 3rd, 2004, 06:12 PM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 27
Time spent in forums: < 1 sec
Reputation 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 
|

June 4th, 2004, 04:37 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
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 
|

June 4th, 2004, 08:42 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

June 4th, 2004, 08:58 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
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 09:05 AM.
|

June 4th, 2004, 09:01 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|