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.

7
  • packaging.python.org/tutorials/packaging-projects Commented Aug 22, 2018 at 0: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. Commented Aug 22, 2018 at 0:30
  • 1
    Why not create a single module and remember its name once and for all? Commented Aug 22, 2018 at 0:30
  • 2
    You still have to import module's from the standard library... Commented Aug 22, 2018 at 0:31
  • Maybe look at the site module or if you are ok with a python 2 solution, look at the user module. Commented Aug 22, 2018 at 0:37

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.