Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to import modules in the parent folder and read resources in the current folder in python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to import the modules in the parent folder and read the resources in the current folder in python". In the daily operation, I believe that many people have doubts about how to import the modules in the parent folder and read the resources in the current folder in python. The editor consulted all kinds of materials and sorted out a simple and useful method of operation. I hope it will be helpful to answer the question of "how to import modules in the parent folder and read the resources in the current folder in python"! Next, please follow the editor to study!

In some special cases, our Python script needs to call other modules in the parent directory. For example:

When writing test cases for GNE, a script, generate_new_cases.py, is placed in the tests folder. The tests folder is placed in the same location as the gne folder. Where the gne folder is a package. I now need to import a class GeneralNewsExtractor in gne from the generate_new_cases.py file.

To simplify the problem, I wrote a separate example of the demonstration. Its file structure and the contents of each file are as follows:

Now, I run run.py directly in the scripts folder with an error prompting me to import from outside the top level of the package.

Now, let's change the code, try to run the code in the parent folder of the scripts folder, and find that there is still an error:

Let's change the code again. In the scripts folder, add the parent folder to the sys.path:

Import sys sys.path.append ('..')

The running effect is shown in the following figure:

This way of writing does work when we run run.py in the scripts folder. But if we run the code again in the parent folder of the scripts folder, it will be wrong again, as shown in the following figure:

To check this reason, let's print the sys.path:

Did you find a very discordant thing: other paths are absolute paths, and finally we added two points that seemed to be very inconsistent. What if we change these two points to absolute paths? So try to get the absolute path of the file that is currently running:

Import sys from pathlib import Path current_folder = Path (_ _ file__). Absolute (). Parent father_folder = str (current_folder.parent) sys.path.append (father_folder)

The running effect is shown in the following figure:

The import module is normal, but reading the resource file is abnormal again.

This is because when import imports the module, it looks for it according to the path in sys.path. However, when reading a resource file, the relative file path is found relative to the workspace.

Now that we execute python3 scripts/run.py at ~ / test_import_father_module, the current workspace is ~ / test_import_father_module. Because the resource file is in the scripts folder, it cannot be found.

So we also need to modify the workspace:

Import os from pathlib import Path current_folder = Path (_ _ file__). Absolute (). Parent os.chdir (str (current_folder))

The running effect is shown in the following figure:

Now it is normal to read the resource file or import the module.

Let's go back to the scripts folder and take a look:

It is found that it can be carried out normally.

The summary involves the environment related to module import, which can be solved by adding an absolute path to the sys.path. The environment involved in reading resource files can be solved by using os.chdir to modify the workspace to another absolute path.

At this point, the study on "how to import modules in the parent folder and read resources in the current folder in python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report