Python 在本地包中导入本地包时遇到问题

Python Trouble Importing Local Package Inside Local Package

这是我第一次 post 在这里,我也是 python 的新手,在过去的两天里,我一直在为错误而苦苦挣扎:

Traceback (most recent call last):
File "Main.py", line 6, in <module>
    import PyScrape
File "/Users/arya/Desktop/Coding/Python/x/x/PyScrape/__init__.py", line 1, in <module>
    from . import ProxyScraper
File "/Users/arya/Desktop/Coding/Python/x/x/PyScrape/ProxyScraper/__init__.py", line 6, in <module>
    from . import FreeProxyList
ModuleNotFoundError: No module named 'ProxyScraper'

当试图导入 "PyScrape" 目录中的 __init__.py 文件中的 "ProxyScraper" 目录时,我的文件结构如下,如果我需要透露更多信息,请告诉我。

PyScrape/
    __init__.py
    ProxyChecker.py
    ProxyScraper/
        __init__.py
        CheckerProxy.py
        FreeProxyList.py
        Hidester.py
        HttpTunnel.py
        PremProxy.py
        SpysMe.py
        Utility.py

"PyScrape" 目录中 __init__.py 的内容如下:

from . import ProxyScraper
from . import ProxyChecker

def Scrape():
     return ProxyChecker.CheckProxyList(ProxyScraper.Scrape())

"ProxyScraper"目录下的__init__.py内容如下: 从线程导入线程 从队列导入队列

from . import FreeProxyList
from . import Hidester
from . import CheckerProxy
from . import SpysMe
from . import HttpTunnel

SourceArray = ["FreeProxyList", "Hidester", "CheckerProxy", "SpysMe", "HttpTunnel"]

def Scrape():
    WorkerList = []
    ProxyQueue = Queue()

    for Source in SourceArray:
        Worker = Thread(name=Source, target=eval(Source + ".Scrape"), args=(ProxyQueue,))
        WorkerList.append(Worker)
        Worker.start()

    for Worker in WorkerList:
        Worker.join()

    return ProxyQueue

对于要导入的 python 模块,其包含目录必须在运行时设置在 PYTHONPATH variable in $HOME/.bashrc file for example, or explicitly inserted in sys.path 中。

一个解决方案是:

>>> import sys
>>> sys.path.insert(0, '/path/to/PyScrape')

另一个是:

$ echo 'export PYTHONPATH=$PYTHONPATH:/path/to/PyScrape' >> $HOME/.bashrc
$ source $HOME/.bashrc

请记住,相关文件可以是 .profile

而不是 .bashrc

当你设置PYTHONPATH那么命令的结果:

>>> import sys
>>> sys.path

将包含您在 $PYTHONPATH 中设置的路径,因为:

sys.path is a list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.