Best Way to Import Other Modules in Python when PYTHONPATH Cannot Be Modified?

First, you should read some of the stuff about Python paths in the following documents.

First, you should read some of the stuff about Python paths in the following documents: docs.python.org/tutorial/modules.html#th... doughellmann.com/PyMOTW/site/index.html To solve your problem you could probably use a pth (python-search-path) file containing list of paths your required modules exist in. Then from your main script you could do import site site. Addsitedir(".") which will automatically append those directories to PYTHONPATH.

First, create a simple main. Py script which provides a single point of entry for your application. For example: if __name__ == '__main__': import sys from package import app sys.

Exit(app.run()) Next, create a top-level package that groups together all your modules, and provide an access point for it in the same directory as main.py. You should then be able to eliminate all the path manipulation code, and simply use fully specified import statements like from package. Module import function from any module within your application.

Thanks for your reply. I'm just wandering what you mean exactly when you say "provide access points". Is this something to do with the init.Py file?

– Alty Oct 28 '11 at 19:55 @Alty. You didn't make it clear in your question to what extent you can restructure the folders your modules are currently in, and I wasn't sure what os you were using - so "access point" just means a plain directory, or a symbolic link, etc.The key thing is finding a way to bring all your modules together under a single package - then things become much simpler to manage (hope I'm not just stating the obvious here). – ekhumoro Oct 28 '11 at 21:59.

You didn't mention what OS you're working on. Much of this answer presupposes a Linux (or Unix-like) environment. If you're on Windows, someone else will have to chime in.It's not clear from your example why you're going through all of these contortions.

First, how are you unable to modify PYTHONPATH? This is just a shell environment variable. If you can log in and run Python, you demonstrably have the necessary access to set your own environment variables.

But I'm not even sure that's necessary. If you were to simply install all of your custom modules into your own library directory, and install . Pth files as suggested by vinilios, you could just do something like this: import site import os site.

Addsitedir(os.path. Expanduser('~/lib/python')) You may also want to take a look into the virtualenv package, which lets you create your own Python environment into which you can install your own packages.It's very handy when you need to install modules that aren't available in the system Python.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions