import via pathname

P

passion_to_be_free

Okay, so in my li'l python script I'm importing a few 3rd party modules
that I have installed on my comp. I need to distribute this script to
several other people, but I won't have access to install the modules on
their comp's. I'm thinking I'll include these modules with my script
and deliver them as a bundle. When I write my script, is there a way to
declare the import statements and specify a relative path to where the
modules are located?

I know this is wrong syntax, but I think it demonstrates what I'm
trying to do:

import myModule path = /modules/myModule
import myModule2 path = /modules/myModule2

Something like that. Is it possible?

-Thx
 
T

Thomas Guettler

Am Mon, 20 Jun 2005 06:31:38 -0700 schrieb passion_to_be_free:
Okay, so in my li'l python script I'm importing a few 3rd party modules
that I have installed on my comp. I need to distribute this script to
several other people, but I won't have access to install the modules on
their comp's. I'm thinking I'll include these modules with my script
and deliver them as a bundle. When I write my script, is there a way to
declare the import statements and specify a relative path to where the
modules are located?

Hi,

You can change sys.path at runtime. Example:

libdir=os.path.join(os.environ["HOME"], "mylibs")
assert(os.path.exists(libdir))
sys.path.insert(0, libdir)

import mylibrary # $HOME/mylib/mylibrary.py

HTH,
Thomas
 
L

Larry Bates

If it is Windows use py2exe and Inno Installer to create an
installation program that does this for you. If it is another
OS, you need to put your modules into a subdirectory of
site-packages and then Python will be able to see your modules.
If you have a lot of modules you might consider turning them
into a package.

-Larry
 
S

Steven Bethard

I know this is wrong syntax, but I think it demonstrates what I'm
trying to do:

import myModule path = /modules/myModule
import myModule2 path = /modules/myModule2

Something like that. Is it possible?

I would put your additional modules into a 'modules' directory with an
__init__.py. For example:

py> os.listdir('.')
['modules']
py> os.listdir('modules')
['my_module1.py', 'my_module2.py', '__init__.py']
py> file('modules/__init__.py').read()
''
py> file('modules/my_module1.py').read()
'name = "module1"\n'
py> file('modules/my_module2.py').read()
'name = "module2"\n'

Then, if you place your python script in the same directory as the
'modules' directory, you can simply import the additional modules from
the 'modules' package:

py> import modules.my_module1 as module1
py> module1.name
'module1'
py> import modules.my_module2 as module2
py> module2.name
'module2'

Note that the 'modules' package needs to be at the same directory level
as your Python module. If you want your 'modules' directory in a
different location, you may want to go the way of Thomas Güttler's
suggestion and modify sys.path. But I probably wouldn't if you don't
have to.

STeVe
 
P

passion_to_be_free

Ahh....I see. I played around with the sys.path function...and it
looks like python automatically looks in the same directory as my
script first. Then is searches to all the other pre-defined paths. So
it works for me to just keep my main script in the same directory as
the two modules I'm using.

Thx!
 
S

Steven Bethard

Ahh....I see. I played around with the sys.path function...and it
looks like python automatically looks in the same directory as my
script first. Then is searches to all the other pre-defined paths. So
it works for me to just keep my main script in the same directory as
the two modules I'm using.

Yup, as long as you're not worried about having too many modules around,
that should work fine. =)

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top