By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,627 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,627 IT Pros & Developers. It's quick & easy.

add new modules?

P: n/a
This is a total newb question, you have been warned...

I've been all over the www.python.org site and googled, but I've not
found just how to add new modules. I've tried setting PYTHONPATH,
I've tried putting the new module directories into the site-packages
directory, I've tried creating the .pth files, I've even done all
three of these things at the same time and still my python script
refuses to import. What is the cannonical way to add new modules
to python? I am running on OS X 10.4 (Macintosh obviously) on basically
freeBSD, os I'm doing UNIX type stuff at the console.

thanks for any help,
DLC
--
================================================== ==========================
* Dennis Clark dl*@frii.com www.techtoystoday.com *
* "Programming and Customizing the OOPic Microcontroller" Mcgraw-Hill 2003 *
================================================== ==========================
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dennis Clark wrote:
This is a total newb question, you have been warned...

I've been all over the www.python.org site and googled, but I've not
found just how to add new modules. I've tried setting PYTHONPATH,
I've tried putting the new module directories into the site-packages
directory, I've tried creating the .pth files, I've even done all
three of these things at the same time and still my python script
refuses to import. What is the cannonical way to add new modules
to python? I am running on OS X 10.4 (Macintosh obviously) on basically
freeBSD, os I'm doing UNIX type stuff at the console.


If you have a single file that you want to make available, put it directly in site-packages. For example put mymodule.py in site-packages, then in code you can say
from mymodule import MyClass

If you have a directory of files that you consider related, that is a package. Put the directory in site-packages, add a file named __init__.py to the directory, and import qualified with the directory name. For example if you have
site-packages/
mystuff/
__init__.py
mymodule.py

then in code you say
from mystuff.mymodule import MyClass

where in both cases MyClass is defined in mymodule.py.

Kent
Jul 19 '05 #2

P: n/a
Dennis Clark wrote:
This is a total newb question, you have been warned...

I've been all over the www.python.org site and googled, but I've not
found just how to add new modules. I've tried setting PYTHONPATH,
I've tried putting the new module directories into the site-packages
directory, I've tried creating the .pth files, I've even done all
three of these things at the same time and still my python script
refuses to import. What is the cannonical way to add new modules
to python? I am running on OS X 10.4 (Macintosh obviously) on basically
freeBSD, os I'm doing UNIX type stuff at the console.


If you are using the Apple-supplied Python 2.3.5, put the modules in
/Library/Python/2.3/site-packages/ . If you are using Bob Ippolito's
Python 2.4.1, then it's
/Library/Frameworks/Python.framework/Versions/Current/lib/python2.4/site-packages
..

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 19 '05 #3

P: n/a
Thanks all,

My problem came when I set PYTHONHOME, apparently that is a bad thing
(in this case). I know that I set it to the directory that python was
in, but something about that was "bad". now everything works.

DLC

Robert Kern <rk***@ucsd.edu> wrote:
: Dennis Clark wrote:
: > This is a total newb question, you have been warned...
: >
: > I've been all over the www.python.org site and googled, but I've not
: > found just how to add new modules. I've tried setting PYTHONPATH,
: > I've tried putting the new module directories into the site-packages
: > directory, I've tried creating the .pth files, I've even done all
: > three of these things at the same time and still my python script
: > refuses to import. What is the cannonical way to add new modules
: > to python? I am running on OS X 10.4 (Macintosh obviously) on basically
: > freeBSD, os I'm doing UNIX type stuff at the console.

: If you are using the Apple-supplied Python 2.3.5, put the modules in
: /Library/Python/2.3/site-packages/ . If you are using Bob Ippolito's
: Python 2.4.1, then it's
: /Library/Frameworks/Python.framework/Versions/Current/lib/python2.4/site-packages
: .

: --
: Robert Kern
: rk***@ucsd.edu

: "In the fields of hell where the grass grows high
: Are the graves of dreams allowed to die."
: -- Richard Harter
--
================================================== ==========================
* Dennis Clark dl*@frii.com www.techtoystoday.com *
* "Programming and Customizing the OOPic Microcontroller" Mcgraw-Hill 2003 *
================================================== ==========================
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.