In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to correctly set up the entry file of the Python project". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
One classmate mentioned the problem that Python could not find the module:
The code structure and screenshots of the code involved are as follows:
The solution to this problem is very simple: just move the start.py file out of the bin folder.
However, if we further analyze this problem, we can see more problems.
In my previous article: why does Python code run but PyCharm draws red lines for me? I talked about the impact of workspaces (Workdir) on code. The workspace identified by PyCharm and VSCode may not be the same as when you run the .py file directly in the terminal window.
Today's problem is essentially a problem caused by the workspace. This student's project root directory is MY_API, so the editor he uses, VSCode, defaults to MY_API as the workspace. So when he writes from lib.interface import server in the start.py file, VScode does not mark him with a red wavy line. Because from VSCode's point of view, the lib folder is really under the workspace.
However, when he runs the start.py file in VSCode, Python runs from under the bin folder. At this point, Python uses the bin folder as a workspace. This is the only start.py file in the workspace, so of course you can't find the lib folder.
If only technically speaking, it is not difficult for you to import other modules under the parent folder of the bin folder. I talked about how to do this in the article "importing modules in the parent folder and reading the resources in the current folder".
But the problem is, you shouldn't do that. You should not put the entry file of the project in a deep folder inside the project.
The so-called entry file means that you have to pass through it before you can reach other files. When you get a Python project, you only need to start with the entry file to read the code, according to the entry file to call the module, along the way, you can read all its implementation logic.
But if you often visit Github, you will find that some people may have contaminated their minds with other junk language. his Python project has five or six folders and seven or eight .py files in the root directory. When you get the project, you don't even know which file python3 xxx.py should run when you want to run the code. You ask around, or after reading the documents for a long time, you know, oh, the original entry file is in com/xx/yy/zz/script/run.py.
When you open the run.py file, you find that at the top of the file, the imported code is written as from.. / aaa import bbb.
It's crazy to write it. I know that some junk languages are popular to write like this. But now you are using Python, be smart and don't write like that.
For a Python project, the entry file should always be on the outermost layer. For example:
When you want to start the project, you can start it directly in the outermost python3 main.py. In main.py, you can import other modules and then call classes or functions in other modules.
What are the benefits of this? In doing so, you started the project at the root of the project, so your workspace is the root of the project. Then you can easily import any other file based on the workspace in any .py file. For example, if you are in the models/mongo-util/mongob_helper.py file and you want to import the time_format () function in utils/abc.py, all you need to do is write this.
From utils.abc import time_format
It is impossible for you to import a module in the parent folder.
Only tool scripts need to be stored in a separate folder, and then call other files in the parent folder. For example, I now have a tool script that reads and writes MongoDB at 0 o'clock every night to clean up invalid data, so at this point, I can create a separate script or tools or bin folder in the root directory, and then put the tool script in, for example:
In this tool script, you may call a function in the models/mongo-util/mongob_helper.py file. In this case, it is acceptable for you to call the contents of the parent folder. But it's just a tool script.
Some students may want to ask, what if my project is a Python package and it does not have an entry file? At this point, you can use the _ _ init__.py of the package as its entry file. You can refer to my code organization structure in GitHub-kingname/GeneralNewsExtractor: general text extractor Beta. [1]. Leave an example.py file in the project root to demonstrate how to call the package. And the code for the package itself is in a folder called gne. This gne folder is a package, and its entry file is in _ _ init__.py.
Ladies and gentlemen, when you write code, think about it first. If someone gets your code and wants to sort out the logic of the project, how do you let him know which file to start with without asking you? In what order should I read it? Can he easily see how the data works in your code?
This is the end of the content of "how to set up the entry file of the Python project correctly". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.