简体   繁体   English

从不同文件夹导入模块

[英]Import modules from different folders

I have following arrangement of files:我有以下文件安排:

    python
     |--main.py
     |files
        |-----folder1
                |-----a.py,a1.py.....
        |-----folder2
                |-----b.py,b1.py....

I wanted to import my modules a and b.py to main.py.我想将我的模块 a 和 b.py 导入到 main.py。 For this I used the following commands in main.py:为此,我在 main.py 中使用了以下命令:

    a = 'C:/python/files/folder1'
    sys.path.insert(0, a)
    from files.folder1 import *

However, I am unable to import modules from folder1 ( similarly for folder2).但是,我无法从文件夹 1 导入模块(文件夹 2 类似)。 I get an error:我收到一个错误:

    No module named files.folder1

I cannot use import command as there are many python files in folder1,folder2...so on.我无法使用导入命令,因为文件夹 1、文件夹 2 中有很多 python 文件......等等。

Am I missing something here?我在这里错过了什么吗?

Quick Answer:快速回答:

Add a file __init__.py (can be blank) to folders files , folder1 and folder2 .将文件__init__.py (可以为空)添加到文件夹filesfolder1folder2 Then you got a package files with sub-packages folder1 and folder2 .然后你得到了一个包含子包folder1folder2的包文件 After this you can import from the main.py like this:在此之后,您可以像这样从 main.py 导入:

from files.folder1 import *

When I do this in Python 2.7 I use:当我在 Python 2.7 中执行此操作时,我使用:

import sys
sys.path.append('C:/python/files/folder1')

import a
import a1

UPDATE更新

Here's a hack I built to import all modules in a directory into a dictionary:这是我构建的一个 hack,用于将目录中的所有模块导入字典:

import os
import sys

dir_of_interest = 'C:/python/files/folder1'
modules = {}

sys.path.append(dir_of_interest)
for module in os.listdir(dir_of_interest):
    if '.py' in module and '.pyc' not in module:
        current = module.replace('.py', '')
        modules[current] = __import__(current)

I just built it and it's extremely hacky but it might be more like something you want.我刚刚构建了它,它非常hacky,但它可能更像是您想要的东西。 So, to access the module you want, instead of saying module_name.thing you would say modules["module_name"].thing所以,要访问你想要的模块,而不是说module_name.thing你会说modules["module_name"].thing

I cannot use import command as there are many python files in folder1,folder2...so on.我无法使用导入命令,因为文件夹 1、文件夹 2 中有很多 python 文件......等等。 Am I missing something here?我在这里错过了什么吗?

I believe the part you are missing is the __init__.py file in each of the folders.我相信您缺少的部分是每个文件夹中的__init__.py文件。 That file should include an __all__ variable that lists all the submodules that will imported by: from somepackage.subpackage import * .该文件应包含一个__all__变量,该变量列出了将通过以下from somepackage.subpackage import *所有子模块: from somepackage.subpackage import *

This is all elegantly explained in the Python Tutorial section on Packages .这一切都在关于 PackagesPython 教程部分中得到了优雅的解释。

Hope this helps :-)希望这可以帮助 :-)

If you add folder1 to the path, that doesn't mean you can import folder1 as a module.如果您将folder1添加到路径中,这并不意味着您可以将folder1作为模块导入。 It means you can import the files inside folder1.这意味着您可以导入文件夹 1 中的文件。 So you could do:所以你可以这样做:

import a
import a1

If you want to have folder1 be a package of which a and a1 are modules, you need to put an __init__.py in folder1 and then do import folder1 .如果您想让folder1成为一个包,其中aa1是模块,则需要在folder1放置一个__init__.py ,然后执行import folder1 If you further want to be able to do from folder1 import * and have that import a and a1 , you need to put code in your __init__.py that imports a and a1 .如果你还希望能够做到from folder1 import * ,并有进口aa1 ,你需要把代码中的__init__.py是进口aa1

If you have a lot of files in a folder that you want to be able to import in a structured way, you should make that folder into a package.如果您希望能够以结构化方式导入文件夹中的大量文件,则应将该文件夹放入一个包中。

Looking at the folder structure , what you could do is , Simply查看文件夹结构,您可以做的是,简单地

from files.folder1 import a , a1
from files.folder2 import b , b1

and this should help这应该有帮助

. .

Extras : I recently thought of a problem , of importing pip packages without actually installing them , then what i did was ... i pip installed the package in a virtual environment and copied only folder of the package name附加:我最近想到了一个问题,即在没有实际安装的情况下导入 pip 包,然后我所做的是...我在虚拟环境中安装了 pip 包并仅复制了包名称的文件夹

eg.例如。 if the package name is qwikidata , then i went to /Lib folder and copied only the qwikidata folder and pasted it in the root of my project , in your case , where the main.py file is and pip uninstalled the package , to avoid the errors如果包名称是 qwikidata ,那么我转到 /Lib 文件夹并仅复制 qwikidata 文件夹并将其粘贴到我的项目的根目录中,在您的情况下, main.py 文件所在的位置并且 pip 卸载了该包,以避免错误

And now i was able to import the package without actually pip installing it since the package was in my root folder of the project现在我可以导入该包而无需实际安装它,因为该包位于我的项目根文件夹中

If the project is not so complicated , then it should be fine , but I wouldn't recommend this way of installing packages .如果项目不是那么复杂,那么应该没问题,但我不推荐这种安装包的方式。

It helps when you want to run your project on some other machine and You dont have an Internet connection .当您想在其他机器上运行您的项目而您没有 Internet 连接时,它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM