Hello guys , this is another post to help show friends how cool you are and also learn along the process. it’s about custom python interpreter prompt and Changing Python interactive prompt
Just another post to make you look cool among peers.
This is for the pythonistas , changing the prompt in the interpreter to whatever you feel like.
So, let’s get to it.
The default prompt in the python interpreter is a 3 greater than sign like ” >>>“. It’s a very boring one and i hate it.
See the image below.
I will be showing how this can be changed to whatever you feel you should be there, instead of the default greater than signs.
This could be your nick name or your code name or a more cool symbols. When i said anything, i mean whatever text you want there. It’s up to you.
The below shows my own python interpreter in the command prompt window. My friends call me “boldman” so i made it show “b0ldman@:”.
This is my choice as i feel that’s more cool than the default signs. Like who doesn’t like to be cool , LoL ?
I should make you know the same thing can be done with the windows command prompt. You can change the CMD prompt, instead of the C:\>.
Read this post on customizing the CMD prompt that as this one is solely for the python interpreter.
We are going to pull this using two method
- Using the environment variable PYTHONSTARTUP
- Using the sitecustomize.py script.
Method 1: Using the environment variable PYTHONSTARTUP
This method has to do with loading a startup script. We are going to place the code to change the prompt in this script.
So every time python starts up , it reads our code and change the prompt.
But what is this code itself to change the prompt ?
The python prompt text is stored in the sys module in a variable named ps1.
For example, lets assume the current computer is for Geekypage, and we want to make the prompt show ‘Geekypage >> ‘ , You know, just to make us look cool.
The below code would just that.
But, the problem here is:
This will change the prompt but upon exiting the interpreter, this changes will be lost and we will be back to the three greater than signs
Sick, right ? So we undoubtedly need a solution to persist the changes.
So what do we do to persist the change ?
This is why we need to set the PYTHONSTARTUP environment variable.
PYTHONSTARTUP is an environment variable holding the name of a script. Python will run this script everytime it starting up making every code in it execute.
- We place our code in a script
- Point PYTHONSTARTUP to the location of this script.
- We are done .
This startup script can be anywhere on your computer but you have to make sure you ‘re pointing the environment variable PYTHONSTARTUP to it because this is what python will look for.
So i will assume you script has been saved to C:\Scripts\prompt_code.py. and the content of your script is :
import sys; sys.ps1 = 'introgeek>> '
Note: introgeek>> should be substituted with the prompt you will like to use.
To set your environment variable
You can simply run the below command in your command prompt provided you are running as an administrator
C:\Windows\system32> setx PYTHONSTARTUPC:\Scripts\prompt_code.py
This will set the environment variable for you or you can just do this through the control panel.
Method 2: The sitecustomize.py script
This method is pretty straight forward.
- Place the code in sitecustomize.py
- We are done.
The glitch here is that you can’t just create a script named sitecustomize.py and place the code in it. That won’t just work.
This file need to exists somewhere, a particular folder on your system for this to work.
So you just need to figure where file should be located on your system as it varies from system to system. Then you can go on, create the script in that location and place the code in the script.
How do we find the location ?
we can get this path by using the python inbuilt site module
>>> import site >>> site.getusersitepackages() 'C:\\Users\\user\\AppData\\Roaming\\Python\\Python36\\site-packages'
In my case, the script should be placed in ‘C:\Users\user\AppData\Roaming\Python\Python36\site-packages’
So you can go on and create the python script – sitecustomize.py – in that directory and place your startup code in it.
import sys; sys.ps1 = "<your prompt goes here>"
I should make you know this startup processes are not mainly for setting a prompt.
You can put whatever code you want in this startups
You can use it for various other thing such as launching a music whenever you launch the python interpreter, showing the current BTC price. Cool right ? Yes, whatever you want.
Look what i did to my python interpreter – printing my nickname in large before prompting me.
So, that’s it.
You want to know how to do the above, hit me up in the comment section.
If you have a suggestion or contribution for this, please drop a comment below and i will be so much glad to respond ASAP.