views:

101

answers:

6

I need to put a python script somewhere on my computer so that in another file I can use it. How do I do this and where do I put it? And where in the python documentation do I learn how to do this? I'm a beginner + don't use python much.


library file: MyLib.py put in a well-known place

def myfunc():
   ....

other file SourceFile.py located elsewhere, doesn't need to know where MyLib.py is:

something = MyLib.myfunc()
+4  A: 

In general, the Modules section of the Python tutorial is a good introduction for beginners on this topic. It explains how to write your own modules and where to put them, but I'll summarize the answer to your question below:

Your Python installation has a site-packages directory; any python file you put in that directory will be available to any script you write. For example, if you put the file MyLib.py in the site-packages directory, then in your script you can say

import MyLib
something = MyLib.myfunc()

If you're not sure where Python is installed, the Stack Overflow question How do I find the location of my Python site-packages directory will be helpful to you.

Alternatively, you can modify sys.path, which is a list of directories where Python looks for libraries when you use the import statement. Your site-packages directory is already in this list, but you can add (or remove) entries yourself. For example, if you wanted to put your MyLib.py file in /usr/local/pythonModules, you could say

import sys
sys.path.append("/usr/local/pythonModules")
import MyLib
something = MyLib.myfunc()

Finally, you could use the PYTHONPATH environment variable to indicate the directory where your MyLib.py is located.

However, I recommend simply placing your MyLib.py file in the site-packages directory, as described above.

Eli Courtwright
+2  A: 

You will have to place your MyLib.py somewhere in your load path (this the paths in your sys.path variable) and then you'll be able to import it fine. Your code would look like

import MyLib
MyLib.myfunc()

Generally speaking, you should distribute your packages using distutils so that they can be easily installed in the proper locations. It would help you as well.

Also, you might not want to install packages in your global Python install. It's customary (and recommended) to use virtualenv which you can use to create small isolated Python environments that can hold local packages.

It's best your give the whole thing a shot and then ask further questions if you have them.

Noufal Ibrahim
+1  A: 

The private version, from my .profile

export PYTHONPATH=${PYTHONPATH}:$HOME/lib/python

which has a subdirectory "msw" so import msw.primes is self documenting or add to a local directory that is already in sys.path

msw
+1  A: 

The Python tutorial section 6 talks about modules, and 6.1.2 talks about the PYTHONPATH, which determines where Python will look for modules you try to import. The tutorial: http://docs.python.org/tutorial/modules.html

Pierce
+3  A: 

Option 1:

Put your file at: <Wherever your Python is>/Lib/site-packages/myfile.py

Add this to your code:

import myfile

Pros: Easy

Cons: Clutters site-packages

Option 2:

Put your file at: /Lib/site-packages/mypackage/myfile.py

Create an empty text file called: <Wherever your Python is>/Lib/site-packages/mypackage/__init__.py

Add this to your code:

from mypackage import myfile

Pros: Reduces clutter in site-packages by keeping your stuff consolidated in a single directory

Cons: Slightly more work; still some clutter in site-packages. This isn't bad for stable stuff, but may be regarded as inappropriate for development work, and may be impossible if Python is installed on a shared drive

Option 3

Put your file in any directory you like

Add that directory to the PYTHONPATH environment variable

Proceed as with Option 1 or Option 2, except substitute the directory you just created for <Wherever your Python is>/Lib/site-packages/

Pros: Keeps development code out of the site-packages directory

Cons: slightly more setup

This is the approach I usually use for development work

Dan Menes
Originally had a typo in Option 2--forgot that SO would interpret underscores as markup. It's fixed now.
Dan Menes
+3  A: 

No one has mentioned using .pth files in site-packages to abstract away the location.

fuzzy lollipop