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

import via pathname

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

--
Thomas GŁttler, http://www.thomas-guettler.de/
Jul 19 '05 #2

P: n/a
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
pa****************@hotmail.com wrote:
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

Jul 19 '05 #3

P: n/a
pa****************@hotmail.com wrote:
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
Jul 19 '05 #4

P: n/a
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!

Jul 19 '05 #5

P: n/a
pa****************@hotmail.com wrote:
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
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.