The recent “Peace Jing” has caused quite a stir.
As a serious programmer, the most afraid of their code online appear all kinds of bugs.
Before you execute any Python code, the Python interpreter will automatically say a prayer of peace, so that the code does not BUG.
Are you ready?
I’m gonna start being a demon. Oh, no, I’m gonna start chanting.
Thanks to Buddha’s blessing, Everything is OK, No bugs in the code.
You must be wondering how this is done?
If you’re familiar with Linux, you know that when you’re remotely logging in to a Linux server using SSH? The.bash_profile file is read to load some environment variables.
.bash_profile you can think of it as a shell script where you can write some shell code for your customization needs.
In Python, there is a file similar to.bash_profile, which normally does not exist.
We need to create a new user environment directory. This directory is quite long and does not need to be memorized. You can get it using the site module method and create it using mkdir -p.
In this directory, create a new usercustomize.py file with the same name as the other one that won’t be recognized.
The contents of usercustomize.py are as follows.
I put this file on my Github and you can go there to get it.
Everything is done, whether you are using Python xxx.py to execute the script
Or use Python to enter the Python Shell, will first say safe safe.
But what else is there to do?
Of course, there is, but relatively speaking, a bit more trouble.
So let’s look at the effect.
Check in ~ / Library/Python / 3.9 / lib/Python/site – packages directory is not usercustomize. Py files.
However, after executing Python and entering Python Shell mode, the safe message is still printed.
How is this done? What a ghost.
The method is actually very simple, as long as you do two things, you can achieve this effect:
The first thing is to create a new Python script in any directory you like, with whatever name you like, such as startup.py, with the same content as above
Second thing, set an environment variable PYTHONSTARTUP to point to your script path
$ export PYTHONSTARTUP=/Users/MING/startup.py
Copy the code
That will do.
But this method only works with the Python Shell, not with Python’s methods of executing scripts.
If I wanted to do this in a script, I’d come up with the crudest and most awkward way to do it — manually load the execution
This article shares two very unusual Python dark magic tricks that can automatically summon the Buddha to recite a peace sutra before you execute any Python code.
I hope you found this post useful. I’ve compiled more information about Python Shell gameplay on my Github (github.com/iswbm/magic…