Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute a function each time you run python shell

Tags:

python

I want to know how (if there is a way) to write a function and have it automatically defined when you start a python shell, or to keep certain defined functions after the shell closes so you don't have to keep re defining it if you use it a lot.

like image 270
jordantoshplus Avatar asked Aug 19 '18 20:08

jordantoshplus


People also ask

How do you execute a function in Python shell?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do I run a Python program repeatedly?

Method 1: Using Time Module sleep() function and make while loop is true. The function will sleep for the given time interval. After that, it will start executing.

How do you call a function repeatedly after a fixed time interval in Python?

start() and stop() are safe to call multiple times even if the timer has already started/stopped. function to be called can have positional and named arguments. You can change interval anytime, it will be effective after next run.

How do I run a Python function from terminal arguments?

You can use the command line arguments by using the sys. argv[] array. The first index of the array consists of the python script file name. And from the second position, you'll have the command line arguments passed while running the python script.


Video Answer


1 Answers

Yes, you can, you need to set your environment variable PYTHONSTARTUP to a python script you would like to be executed at every python startup.

By convention, such files are named by adding the rc suffix to the program name you're trying to tweak, and are usually located at the root of your home directory. In that case I would create the python file under $HOME/.pythonrc and then run export PYTHONSTARTUP=~/.pythonrc (for UNIX-like systems, it could be slightly different on Windows if you don't have a MinGW or equivalent).

Here's an example of .pythonrc file that you can play with: https://gist.github.com/twneale/5245670

like image 62
gogaz Avatar answered Oct 19 '22 16:10

gogaz