4

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()
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Jason S
  • 184,598
  • 164
  • 608
  • 970

7 Answers7

8

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

Community
  • 1
  • 1
Dan Menes
  • 6,667
  • 1
  • 33
  • 35
4

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.

Community
  • 1
  • 1
Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
3

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

2

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
  • 71,383
  • 13
  • 135
  • 169
1

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
  • 42,753
  • 9
  • 87
  • 112
1

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
  • 346
  • 1
  • 4
0

In Windows: You can make use of SYMLINK available in W10.

go to <Wherever your Python is>/Lib/site-packages/

mklink [your_python_file_name].py <your_lib_folder>/[your_python_file_name].py 

*tested with python 3.8, 3.10 and 3.11

PROS: You can use multiple python versions

CONS: You have to do this for every version.

*Disclaimer, not intended for production use.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – xlmaster Mar 03 '23 at 17:51