In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces you to write Python without import method tutorial, the content is very detailed, interested friends can refer to, hope to be helpful to you.
When writing Python programs, we need to rely on some libraries, so at first we always:
Import xxx as xxx from xxx import xxx
We sometimes write that when we find that we need to introduce new libraries, we have to go back to the front and import again. If you are using an editor similar to jupyter, you have to run the code block after you add the import statement.
These operations are often repetitive. For example, when we play with data visualization, we always go to import-related libraries:
Import pandas as pd import numpy as np import seaborn as sns...
At this time, someone thought, wow, what a hassle, can you import these libraries by yourself every time? You are an adult, can you automatically import it to me when I write the code to call the relevant library? Don't make a mistake!
Like this:
As you can see, there is no need to import in advance here, but when using related libraries, automatically help us import on the first line, happiness ah!
How did you get it?
The next thing Shuai b wants to share with you is Pyforest, a library that can automatically help you import.
This library encapsulates some of the "import statements" needed for popular data analysis.
It has the function of "lazy loading", that is, when you install it and you use the relevant libraries in your code, it will help you import automatically. And those encapsulated import statements will not help you import in when you don't use them.
You can install a wave through pip:
Python pip install-upgrade pyforest
After the installation is complete, pyforest_autoimport.py will be added to your IPython startup configuration by default, so that we can use its features directly when using IPython or Jupyter, and you don't even need import when writing code:
Import pyforest
For example, when you have finished installing, you can directly open IPython and use the relevant libraries without writing import statements:
As you can see, my operation here is to directly use the abbreviation of the library, because when people write LazyImport, they have arranged it according to our habit of writing code:
How's it going?
Of course, the encapsulated import statements are more related to data visualization, so if you want to add some other import statements, how to do it?
Simply go to the pyforest you installed and add it to user_imports.py. The general path is as follows:
~ / .pyforest/user_imports.py
For example, I add an import requests statement here:
Then I can directly request without importing requests:
You can add import statements to user_imports.py according to your own habits.
Mom doesn't have to worry anymore. She needs to write import every time she relies on the library.
This library is suitable for use in Jupyter Notebook, Jupyter Lab or IPython. If you want to use it elsewhere, you can import Pyforest first, and then use your encapsulated import statement, like this:
This is the end of the tutorial on how to write Python without import. I hope the above content can be of some help and can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.