且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python中从子文件夹导入

更新时间:2023-10-23 14:24:16

请注意,Folder1是目录,.py脚本是您的模块.

Note that Folder1 is a directory, the .py scripts are your modules.

pyscript3中,您应该可以:

from Folder1 import pyscript1

然后,您可以访问名称为methodname的方法,例如:

Then you can access a method with name methodname like:

pyscript1.methodname()

否则,您可以直接导入该方法,例如:

Otherwise you can import the method directly like:

from Folder1.pyscript1 import methodname

并像这样使用它:

methodname()

要让程序查看Folder1Folder2,您需要从MainProject文件夹运行程序.

For your program to see Folder1 and Folder2, you need to run your program from the MainProject folder.

pyscript3移至MainFolder或编写另一个脚本,将其称为main.py,然后调用必要的代码以实例化该类/在pyscript3.py中调用所需的函数.

Either move pyscript3 to your MainFolder or write another script, let's call it main.py, and call the necessary code to instantiate the class/call the function you want in pyscript3.py.

总而言之,您始终希望从项目的基本文件夹中运行入口模块.

To summarize, you always want to run the entry module from the base folder of your project.