需要帮助来了解有关Python中相对导入和绝对导入的问题 [英] Need help understanding this issue regarding relative vs absolute imports in Python

查看:56
本文介绍了需要帮助来了解有关Python中相对导入和绝对导入的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想在特定文件夹中安装某个Python软件包(使用pip install -t),然后从该软件包导入模块时,出现错误.

I was getting an error when I wanted to install a certain Python package in a specific folder (using pip install -t), and then import a module from that package.

我将问题发布在软件包的Github上,结果证明我无法在特定文件夹中安装软件包;不支持以这种方式安装软件包.

I posted the issue on the package's Github and it turns out I can't install a package in a certain folder; that it isn't supported for the package to be installed that way.

即使没有google合作,我也可以尝试使用pip install -t sklearnFolder在某些特定文件夹中安装sklearn,然后将其作为sklearnFolder.sklearn.manifold导入时重现导入错误.

Even without google collab, I can reproduce the import error when trying to install sklearn in some specific folder with pip install -t sklearnFolder then importing it as sklearnFolder.sklearn.manifold.

这不是安装/使用scikit-lean的受支持方法.不能正常工作的原因之一是scikit-learn中的一些模块使用了绝对导入(例如从sklearn导入某些内容),但这种设置会失败.

That is not a supported way of installing / using scikit-lean. One reason why it wouldn't work is that a few modules in scikit-learn use absolute imports (e.g. from sklearn import something) which will fail with such setup.

您应该使用pip install进行安装,或者如果您希望将其安装在某个特定文件夹中,则将存储库克隆到该文件夹​​,然后运行pip install -e,在两种情况下,它都将作为sklearn导入.

You should either install it with pip install or if you want to have it in some specific folder, clone the repo to that folder, then run pip install -e , in both cases it will be imported as sklearn.

来自 https://github.com/scikit-learn/scikit-学习/问题/11656

我不太理解.

我想过

from folderName.package import module

from package import module

因为它们都是绝对进口.与之相同,它们都完全指定了导入模块的路径.因此,我的理解有些不足,但我不知道这是什么.

Because they're both absolute imports. As in, they both completely specify the path of the imported module. So there is something off about my understanding but I don't know what it is.

推荐答案

在导入中,您没有指定folderName作为包的前缀.如果软件包已安装或在python路径中,则只需使用软件包名称进行导入.

In an import you do not specify the folderName to prefix a package. If the package is installed or in the python path, then you just use the package name to import.

    # Assume the below structure is under a directory (folder) called /app/home/packages.

    reservation/                  This is your top-level pacakge 
        __init__.py               Initialize the package
        hotels/                   Subpackage for hotel reservations
            __init__.py
            slots.py
            bid.py
            demand.py
            ...
            restaurents/                  Another Subpackage under hotels 
                __init__.py
                cuisine.py
                hours.py
                tableslots.py
                ...
        rewards/                      Subpackage for rewards
            __init__.py
            points.py
            discounts.py
            membersonly.py
            ...

由于该软件包位于/app/home/packages下,因此以下导入无效,因为您为文件夹名称加上前缀.

As the package is under /app/home/packages, then the following import is NOT valid as you prefix the folder name.

    from packages.reservation import hotels 

正确的导入方法是从实际的软件包中进行初始化的__init__.py软件包.如果在示例中看到,则保留文件夹具有__init__.py.

The correct way to import is from the actual package which has the package initialization __init__.py. If you see in the example, reservation folder has __init__.py.

    from reservation import hotels 

如果要导入酒店下的子模块,则将在包前面加上前缀:

If you want to import the sub module under the hotels, then you will prefix with the package:

   from reservation.hotels import restaurents

或者,您可以直接导入子模块,但在使用子模块时必须在其前面加上前缀:

Alternatively you can import directly the sub module but you will have to prefix with the package when you use it:

   import reservation.hotels.restaurents

这篇关于需要帮助来了解有关Python中相对导入和绝对导入的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆