Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: how/where to put a simple library installed in a well-known-place on my computer

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()
like image 958
Jason S Avatar asked Dec 01 '22 05:12

Jason S


2 Answers

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

like image 100
Dan Menes Avatar answered Dec 06 '22 10:12

Dan Menes


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.

like image 28
Eli Courtwright Avatar answered Dec 06 '22 10:12

Eli Courtwright