gpt4 book ai didi

Python 在本地包中导入本地包时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:36:50 25 4
gpt4 key购买 nike

这是我在这里发表的第一篇文章,我对 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例如,$HOME/.bashrc 文件中的变量,或显式插入 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.

关于Python 在本地包中导入本地包时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53195114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com