简体   繁体   English

Python3 模块导入混淆

[英]Python3 module import confusion

When I install a python module (for example mediafile ) with pip I can import it like this:当我使用 pip 安装 python 模块(例如mediafile )时,我可以像这样导入它:

from mediafile import MediaFile

and it works perfectly.它完美无缺。

But then I install it to a different location ( pip install --target="C:/msys64/home/myname/myprogram/tools/mediafile/" mediafile ), I can only import it like this:但是后来我将它安装到不同的位置( pip install --target="C:/msys64/home/myname/myprogram/tools/mediafile/" mediafile ),我只能像这样导入它:

from tools import mediafile

and importing MediaFile just doesn't work.并且导入MediaFile是行不通的。 (I tried from tools.mediafile import MediaFile and a couple of other variations with no success). (我尝试from tools.mediafile import MediaFile和其他几个变体但没有成功)。

Here's an ouptut :这是一个输出:

ImportError: cannot import name 'MediaFile' from 'tools.mediafile' (unknown location)

When I try to use mediafile.MediaFile it gives me this error :当我尝试使用mediafile.MediaFile它给了我这个错误:

AttributeError: module 'tools.mediafile' has no attribute 'MediaFile'

Any idea where I got the syntax wrong?知道我在哪里弄错了语法吗?

The command pip install --target=/path/to/package mypackage will install the package inside the directory you have specified, ie at /path/to/package/mypackage .命令pip install --target=/path/to/package mypackage您指定的目录中安装包,即/path/to/package/mypackage In your case it is probably at C:/msys64/home/myname/myprogram/tools/mediafile/mediafile .在您的情况下,它可能位于C:/msys64/home/myname/myprogram/tools/mediafile/mediafile

If this is the case you should be able to import it with:如果是这种情况,您应该能够使用以下命令导入它:

from tools.mediafile.mediafile import MediaFile

but don't do this - instead you should delete it and reinstall with但不要这样做 - 相反,您应该删除它并重新安装

pip install --target="C:/msys64/home/myname/myprogram/tools/" mediafile

Then you should be able to import it with然后你应该能够导入它

from tools.mediafile import Mediafile

The problem with the above approach, as you have discovered, is that packages will expect to be able to import their own dependencies just using import dependency - they will not know about your tools directory.正如您所发现的,上述方法的问题在于,包希望能够仅使用import dependency来导入它们自己的import dependency ——它们不会知道您的tools目录。 To fix this, and to make your own imports easier, you will need to add the directory to your PYTHONPATH environment variable.为了解决这个问题,并使您自己的导入更容易,您需要将该目录添加到您的PYTHONPATH环境变量中。 See eg this question for how to do this in Windows.有关如何在 Windows 中执行此操作的信息,请参见例如此问题

Alternatively you can add it inside the script itself:或者,您可以将其添加到脚本本身中:

import sys
sys.path.append("tools")
from mediafile import MediaFile

However setting PYTHONPATH is the preferred way to do this.但是,设置 PYTHONPATH 是执行此操作的首选方法。

Note that I have assumed you deleted and reinstalled mediafile as above, so your directory structure should be:请注意,我假设您如上所述删除并重新安装了mediafile ,因此您的目录结构应该是:

tools
├── mediafile.py
├── mutagen
├── ...

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

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