Python无法在本地包内导入本地包

时间:2018-11-07 17:58:05

标签: python python-3.x python-import python-module python-packaging

这是我在这里的第一篇文章,并且我对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

1 个答案:

答案 0 :(得分:0)

要导入python模块,必须在$HOME/.bashrc文件的MS docs变量中设置其包含目录,或者在运行时将其显式插入PYTHONPATH。 >

一种解决方案是:

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

另一个可能是:

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

请记住,相关文件可能是.bashrc,而不是.profile

设置PYTHONPATH后,命令的结果:

>>> import sys
>>> sys.path

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

  

sys.path是一个字符串列表,用于指定模块的搜索路径。从环境变量PYTHONPATH初始化,再加上依赖于安装的默认值。