0

Is there a way to add functions I create to the Python standard library on my local machine?

I come from the matlab world where things aren't really efficient and fast but there are looooads of functions at my fingertips without having to import their files. My problem is that, if I make a function in Python and want to use it, then i will need to also remember the module its in. My memory is shite. I understand that Python is structured that way for efficiency but if I'm adding only a handful of functions to the standard library that I consider very important, I'd guess that the impact to the performance is practically negligible.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
shakeNbake
  • 37
  • 5
  • https://packaging.python.org/tutorials/packaging-projects/ – Joshua Schlichting Aug 22 '18 at 00:29
  • 1
    Why don't you just save all your custom functions to `myFunctions.py` and add `from myFunctions import *` in every python program. Then you don't need to worry about it. – Kevin Fang Aug 22 '18 at 00:30
  • 1
    Why not create a single module and remember its name once and for all? – John Coleman Aug 22 '18 at 00:30
  • 2
    You still have to import module's from the standard library... – juanpa.arrivillaga Aug 22 '18 at 00:31
  • Maybe look at the [site module](https://docs.python.org/3.5/library/site.html#module-site) or if you are ok with a python 2 solution, look at the [user module](https://docs.python.org/2/library/user.html#module-user). – The Matt Aug 22 '18 at 00:37
  • I don't think you mean "standard library" here. The standard library is the collection of modules and packages that come with Python. Adding things to the stdlib is kind of pointless, because there's no visible difference between stdlib modules and modules installed into site-packages. Maybe you mean "builtins"? – abarnert Aug 22 '18 at 00:45
  • Also, do you want stuff to be available (1) in all of your scripts, (2) only in interactive interpreter sessions, or (3) only in IPython/Jupyter sessions? Because there are great ways to customize things in IPython, from a startup file to dumping and resuming whole sessions, and there are limited versions of some of that in the normal interactive interpreter, but for all scripts there's only `site`. – abarnert Aug 22 '18 at 00:47

2 Answers2

3

Python has a namespace called __builtins__ in which you can stick stuff that you want available all the time. You probably shouldn't, but you can. Be careful not to clobber anything. Python won't stop you from using the same name as a built-in function, and if you do that, it'll probably break a lot of things.

# define function to always be available
def fart():
    print("poot!")

__builtins__.fart = fart

# make re module always available without import
import re
__builtins__.re = re

Now the question is how to get Python to run that code for you each time you start up the interpreter. The answer is usercustomize.py. Follow these instructions to find out where the correct directory is on your machine, then put a new file called usercustomize.py in that directory that defines all the stuff you want to have in __builtins__.

There's also an environment variable, PYTHONSTARTUP, that you can set to have a Python script run whenever you start the interpreter in interactive mode (i.e. to a command prompt). I can see the benefit of e.g. having your favorite modules available when exploring in the REPL. More details here.

kindall
  • 178,883
  • 35
  • 278
  • 309
2

It sounds like you want to create your own packages & modules with tools you plan on using in the future on other projects. If that is the case, you want to look into the packaging your own project documentation: https://packaging.python.org/tutorials/packaging-projects/

You may also find this useful:

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54