Python: how / where to put a simple library installed in a well known location on my computer
I need to place a python script somewhere on my computer so that in another file I can use it. How can I do this and where can I put it? And where in the python documentation do I learn how to do this? I'm a newbie + don't use python much.
library file: MyLib.py has been placed in a known location
def myfunc():
....
another SourceFile.py file located elsewhere doesn't need to know where MyLib.py is:
something = MyLib.myfunc()
a source to share
Option 1:
Place your file at:
<Wherever your Python is>/Lib/site-packages/myfile.py
Add this to your code:
import myfile
Pros: Easy
Cons: site-packages Clutters
Option 2:
Place your file at: / lib / site -packages / mypackage / myfile.py
Create an empty text file:
<Wherever your Python is>/Lib/site-packages/mypackage/__init__.py
Add this to your code:
from mypackage import myfile
Pros: Reduces site bundle clutter by keeping your data in the same directory
Cons: a little more work; still some mess in site packages. This is not bad for stable things, but may be considered unacceptable for development, and may not be possible if Python is installed on a shared disk.
Option 3
Place your file in any directory you like
Add this directory to your PYTHONPATH environment variable
Proceed in the same way as for Option 1 or Option 2, except to replace the newly created directory for <Wherever your Python is>/Lib/site-packages/
Pros: Saves development code from the package site directory
Cons: slightly more customization
This is the approach I usually use for development
a source to share
In general, the Modules section of the Python manual is a good introduction for newbies 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 directory site-packages
; any python file you put in this directory will be accessible to any script you write. For example, if you put a file MyLib.py
in a directory site-packages
, then in your script you can say
import MyLib something = MyLib.myfunc()
If you don't know where Python is installed, you might find Stack Overflow problem helpful How to find the location of my Python sites sites directory .
Alternatively, you can change sys.path , which is a list of directories in which Python looks for libraries when you use an import
expression. Your directory is site-packages
already on this list, but you can add (or remove) entries yourself. For example, if you want to put your file MyLib.py
in /usr/local/pythonModules
, you can say
import sys
sys.path.append("/usr/local/pythonModules")
import MyLib
something = MyLib.myfunc()
Finally, you can use the PYTHONPATH environment variable to point to the directory where your MyLib.py
.
However, I recommend just placing your file MyLib.py
in a directory site-packages
as described above.
a source to share
You need to place MyLib.py
yours somewhere in your load path (this is the path in your variable sys.path
) and then you can import it ok. Your code will look like
import MyLib
MyLib.myfunc()
Generally speaking, you should distribute your packages using distutils so that they can be easily installed in the appropriate locations. This will help you.
Also, you may not want to install packages in your global Python installation. It is common (and recommended) to use virtualenv , which you can use to create small isolated Python environments that can contain local packages.
Your best bet is to give it all away and then ask additional questions if you have any.
a source to share
Section 6 of the Python tutorial talks about modules, and section 6.1.2 talks about the PYTHONPATH, which determines where Python will look for the modules you are trying to import. Tutorial: http://docs.python.org/tutorial/modules.html
a source to share