简体   繁体   English

无法使用相对导入从当前目录导入 urllib3

[英]Can't import urllib3 from the current directory using relative imports

When I am trying to run a compiled Python executable, I am running into a weird problem with the requests module.当我尝试运行已编译的 Python 可执行文件时,我遇到了 requests 模块的奇怪问题。

The exact Error message is as follows:确切的错误信息如下:

  File "tradingbot.py", line 11, in init tradingbot
  File "C:\Users\igors\Downloads\olympian_(4)\bin3\lib\site-packages\requests\__init__.py", line 60, in <module>
    from .packages.urllib3.exceptions import DependencyWarning
  File "C:\Users\igors\Downloads\olympian_(4)\bin3\lib\site-packages\requests\packages\__init__.py", line 29, in <module>
    import urllib3
ImportError: No module named urllib3

As you can see, __init__.py file cannot import urllib3 module.如您所见, __init__.py init__.py 文件无法导入 urllib3 模块。

After following the path, we see the following directory:跟随路径后,我们看到以下目录:

在此处输入图片说明

After that, I decided to look into __init__.py , and found the following import code:之后,我决定查看__init__.py ,并找到以下导入代码:

try:
    from . import urllib3
except ImportError:
    import urllib3
    sys.modules['%s.urllib3' % __name__] = urllib3

As you can see, the line that Error message refers to is import urllib3 .如您所见,错误消息所指的行是import urllib3

The problem is, why the program struggles with finding a folder containing the urllib3 module which is clearly located in the same directory as init file?问题是,为什么程序很难找到包含 urllib3 模块的文件夹,该文件夹显然与init文件位于同一目录中?

Please keep in mind that the program should work without having Python or any other libraries pre-installed on the user's PC.请记住,该程序应该可以在没有 Python 或任何其他预装在用户 PC 上的库的情况下运行。 It should collect all the files from its own resources.它应该从自己的资源中收集所有文件。

PS People asked me to show the urllib3 folder contents, so here it is: PS 人们让我显示 urllib3 文件夹内容,所以这里是:

在此处输入图片说明

Probably int the urllib3 folder there is a .py file you need to import.可能在 urllib3 文件夹中有一个您需要导入的 .py 文件。 Or you can just import everything at once.或者您可以一次导入所有内容。 You can do those things respectively like this:你可以像这样分别做这些事情:

import urllib3.[file you want to import]
from urllib3 import *

usually python module should contain __init__.py itself通常 python 模块应该包含__init__.py本身

could you provide more information on what your urllib3 folder contains?你能提供更多关于你的urllib3文件夹包含什么的信息吗?

as well, you could try to take a look at https://docs.python.org/3/library/venv.html or https://www.docker.com/ if you don't want to install anything in pc同样,如果您不想在电脑上安装任何东西,您可以尝试查看https://docs.python.org/3/library/venv.htmlhttps://www.docker.com/

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

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