In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about Python Black how to format and beautify the code with one click. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
When copying other people's code for use, the most annoying thing is to see all kinds of red and orange lines displayed on the editor, right? Especially those codes that do not comply with the PEP8 rules, the spaces on both sides of the equal sign will be added to your soft hand. I really hope that one day the little friends who write Python all over the world can unify the code specification.
Nothing, now that we have the artifact of Black, we can automatically format the code by entering a command in the terminal! And for beginners, using Black to learn code specifications is also a very good choice.
1. Prepare for
Before you begin, make sure that Python and pip are successfully installed on your computer. Oh, if not, please visit this article: hyperdetailed Python installation Guide for installation. If you use Python for data analysis, you can directly install Anaconda:Python data analysis and mining helper-Anaconda
Open Cmd (start-run-CMD) in Windows environment, and open Terminal (command+ space input Terminal) in Apple system environment, ready to start entering commands to install dependencies.
Of course, I recommend that you use the VSCode editor, Copy the code of this article, and install the dependency module by running commands in the terminal below the editor. What a comfortable thing: the best partner of Python programming-VSCode detailed guide.
Enter the following command at the terminal to install the dependent module we need:
Pip install black
If you see Successfully installed xxx, the installation is successful.
After the installation is complete, you have two ways to use black. One is to enter it directly in the command:
Black your file path .py
It will automatically format the Python file, if you are using VSCode, I recommend using this method to format.
If you use Pycharm, you need to set this up:
On the PyCharm menu File- > Setting- > Tools- > External Tools- > Click the button "+" sign:
Enter a name in Name in the pop-up window as the name to be displayed later in the Pycharm menu-> Tools- > External Tools, as a convenient tool entry, for example, enter the name BlackFormatter here:
Enter the directory where the black.exe file after black installation is located in Programs. The directory is found in the cmd window of Windows through the command where black and in Linux with the command which black.
For example, my path is: C:\ Users\ Ckend\ Anaconda3\ Scripts\ black.exe, fill in the Programs box with the full path containing the balck.exe file name. Fill in $FilePath$, in Arguments and $ProjectFileDir$ in Working Directory. Then click OK.
two。 Use
As mentioned earlier, there are two ways to use Vscode, such as the following bad format:
It's true that all kinds of lines appear, but you just need to type in the terminal:
Black test.py
You can beautify the code format that conforms to the PEP8 standard with one click:
It is more convenient to use Pycharm (if you have configured it), you just need to press Tools- > External Tools- > BlackFormatter (the name you entered when you just configured it) to format the current file directly.
The formatting is successful as shown in the figure below the Pycharm:
Pycharm can also be formatted as a command, which can be typed in Terminal, because it is actually the same thing as the terminal of Vscode. Just note that the environment installed by black is consistent with the virtual environment used by your terminal.
The above is the editor for you to share the Python Black how to format and beautify the code, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.