简体   繁体   English

从另一个文件夹导入失败python

[英]Fail python import from another folder

I am experimenting with python, mostly troubleshooting other people's code. 我正在尝试使用python,主要是对其他人的代码进行故障排除。 I am trying to get a program to run, " path\\folderA\\program.py ". 我试图让程序运行,“ path\\folderA\\program.py ”。

I am running the program from path\\folderA 我正在从path\\folderA运行该程序

I am getting an error: 我收到一个错误:

ImportError: No module named fff.ggg.ppp

program.py contains an import: program.py包含一个导入:

from fff.ggg.ppp import mmm

In the folder "path\\folderB" there are: " path\\folderB\\fff\\__init__.py " " path\\folderB\\fff\\ggg " 在文件夹“path \\ folderB”中有:“ path\\folderB\\fff\\__init__.py ”“ path\\folderB\\fff\\ggg

folder ggg also contains __init__.py , as well as program ppp.py 文件夹ggg还包含__init__.py ,以及程序ppp.py

From reading other posts, like Python error "ImportError: No module named" I understand that having the __init__.py makes a folder a "package" which makes imports from it possible - but it doesn't work, since I am getting an error. 从阅读其他帖子,如Python错误“ImportError:没有模块命名”我明白让__init__.py使文件夹成为一个“包”,使得从它导入 - 但它不起作用,因为我收到一个错误。

This has been working for other people that worked with these projects, so there is something wrong with my setup. 这一直适用于使用这些项目的其他人,所以我的设置有问题。

I read something about the directories having to be in the sys.path. 我读了一些关于必须在sys.path中的目录的内容。 Does that mean I have to add them to the environment variable path ? 这是否意味着我必须将它们添加到环境变量路径中? That would mean adding a lot of directories to the PATH though, so it can't be. 这意味着要向PATH添加很多目录,所以它不可能。

So I also found the following: 所以我也发现了以下内容:

import sys
sys.path.append( <path to FolderB> )

But that means changing the code (which has not been necessary for other people) and hard-coding a path to what it is on my local machine - which I shouldn't have to, right ? 但这意味着更改代码(对于其他人来说并不是必需的)并且硬编码到我本地机器上的路径 - 我不应该这样做,对吧?

I can't visualize it - apparently I am not supposed to change the code and hard-code the physical path to the import module - so how can a program from folderA even know to look in folderB for an import ? 我无法想象它 - 显然我不应该更改代码并硬编码导入模块的物理路径 - 那么来自folderA的程序怎么能知道在folderB中查找导入?

How does the magic of __init__.py work ? __init__.py的魔力是如何起作用的?

I can't visualize it - apparently I am not supposed to change the code and hard-code the physical path to the import module - so how can a program from folderA even know to look in folderB for an import ? 我无法想象它 - 显然我不应该更改代码并硬编码导入模块的物理路径 - 那么来自folderA的程序怎么能知道在folderB中查找导入?

You are correct. 你是对的。 Somehow you have to tell python to look for imported modules in folderB. 不知何故,你必须告诉python在folderB中查找导入的模块。 There is no __init__.py magic that lets you import from other folders on your hard drive. 没有__init__.py魔术可以让你从硬盘上的其他文件夹导入。

Usually, if you've got various different python packages like that, they work by being installed into python's library. 通常,如果你有各种不同的python包,那么它们可以安装到python的库中。 That way they can imported from anywhere. 这样他们就可以从任何地方导入。 This is usually accomplished by a setup.py script. 这通常由setup.py脚本完成。 Check if folderB has one. 检查folderB是否有一个。 Run it with python setup.py install . 使用python setup.py install运行它。

If that doesn't work, we'll need more information about how this code is structured. 如果这不起作用,我们将需要有关此代码结构的更多信息。

Folder B must be on the sys.path, so you would either need to move mmm to A, or modify sys.path from within A (not sure if that works). 文件夹B必须位于sys.path上,因此您需要将mmm移动到A,或者从A中修改sys.path(不确定是否可行)。 __init__.py tells python that the folder is a package, so you could have folders with __init__.py within folders with __init__.py and python treats the folders inside as parts of the parent folder. __init__.py告诉python该文件夹是一个包,所以你可以在__init__.py的文件夹中包含带__init__.py的文件夹,python将里面的文件夹视为父文件夹的一部分。 Check out sympy or almost any large python library and you will find such a structure. 查看sympy或几乎任何大型python库,你会发现这样的结构。 It can also contain code to be run on import, but can also be empty. 它还可以包含要在导入时运行的代码,但也可以为空。

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

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