Python 自建Module导入错误

能源用到了一款基于modbus tcp协议的网关,为了能正常的接收转发数据。搜索了一下现有的各种modbus的库,发现文档都非常少,这就让人很抑郁啊。关键是对于硬件是真的不懂啊,就是个软件的程序媛,现在要对接硬件。这不是蛋疼了吗~~

终于最后找到一款工具https://pypi.org/project/modbus-tcp-server/,测试了一下效果还ok。最起码能收到数据啦,剩下的就是怎么把数据转发出去,或者写入数据库的问题。

这俩就没那么麻烦了。本来想基于这个库直接修改,但是复制出来运行会出现各种的异常:

(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
  File ".\mdbus_server.py", line 6, in <module>
    from modbus_tcp_server.network import ModbusTCPServer
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
  File ".\mdbus_server.py", line 6, in <module>
    from .network import ModbusTCPServer
ImportError: attempted relative import with no known parent package
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
  File ".\mdbus_server.py", line 6, in <module>
    from .network import ModbusTCPServer
ImportError: attempted relative import with no known parent package
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
  File ".\mdbus_server.py", line 6, in <module>
    from .network import ModbusTCPServer
ImportError: attempted relative import with no known parent package
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
  File ".\mdbus_server.py", line 6, in <module>
    from modbus_tcp_server.network import ModbusTCPServer
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
  File ".\mdbus_server.py", line 6, in <module>
    from modbus_tcp_server.network.accept_thread import ModbusTCPServer
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
  File ".\mdbus_server.py", line 6, in <module>
    from modbus_tcp_server.network.accept_thread import ModbusTCPServer
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
  File ".\mdbus_server.py", line 6, in <module>
    from modbus_tcp_server.network.accept_thread import ModbusTCPServer
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
  File ".\mdbus_server.py", line 6, in <module>
ImportError: attempted relative import with no known parent package
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
  File ".\mdbus_server.py", line 6, in <module>
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\__init__.py", line 1, in <module>
    from modbus_tcp_server.network.accept_thread import ModbusTCPServer
ModuleNotFoundError: No module named 'modbus_tcp_server'
Traceback (most recent call last):
  File ".\mdbus_server.py", line 6, in <module>
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from modbus_tcp_server.network.conn_thread import ConnectionThread
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\conn_thread.py", line 8, in <module>
    from ..datagrams import MODBUSTCPMessage
ValueError: attempted relative import beyond top-level package
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\conn_thread.py", line 8, in <module>
    from ..datagrams import MODBUSTCPMessage
ValueError: attempted relative import beyond top-level package
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\conn_thread.py", line 8, in <module>
    from modbus_tcp_server.network.datagrams import MODBUSTCPMessage
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\conn_thread.py", line 8, in <module>
    from datagrams import MODBUSTCPMessage
ModuleNotFoundError: No module named 'datagrams'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from datagrams import *
ModuleNotFoundError: No module named 'datagrams'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\conn_thread.py", line 8, in <module>
    from datagrams import MODBUSTCPMessage
ModuleNotFoundError: No module named 'datagrams'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from modbus_tcp_server.network.datagrams import *
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\modbus_tcp_server> cd ..\baby_modbus_tcp_server\
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py     
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from modbus_tcp_server.network.datagrams import *
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from baby_modbus_tcp_server.network.datagrams import *
ModuleNotFoundError: No module named 'baby_modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from baby_modbus_tcp_server.network.datagrams import *
ModuleNotFoundError: No module named 'baby_modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
.
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from baby_modbus_tcp_server.network.datagrams import *
ModuleNotFoundError: No module named 'baby_modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py

Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from baby_modbus_tcp_server.network.datagrams import *
ModuleNotFoundError: No module named 'baby_modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\mdbus_server.py
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from baby_modbus_tcp_server.network.datagrams import *
ModuleNotFoundError: No module named 'baby_modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from baby_modbus_tcp_server.network.datagrams import *
ModuleNotFoundError: No module named 'baby_modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server
Traceback (most recent call last):
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from baby_modbus_tcp_server.network.datagrams import *
ModuleNotFoundError: No module named 'baby_modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server
Traceback (most recent call last):
  File ".\mdbus_server.py", line 9, in <module>
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
    from baby_modbus_tcp_server.network.datagrams import *
  File "F:/Pycharm_Projects/Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:/Pycharm_Projects/Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
  File "F:/Pycharm_Projects/Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 11, in <module>
    from exceptions import InvalidFrame
ModuleNotFoundError: No module named 'exceptions'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server
Traceback (most recent call last):
  File ".\mdbus_server.py", line 9, in <module>
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
    from .conn_thread import ConnectionThread
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\conn_thread.py", line 9, in <module>
    from baby_modbus_tcp_server.network.datagrams import *
  File "F:/Pycharm_Projects/Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:/Pycharm_Projects/Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 9, in <module>
    from modbus_tcp_server.network.datagrams import MODBUSTCPMessage
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server
Traceback (most recent call last):
  File ".\mdbus_server.py", line 9, in <module>
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 7, in <module>
    from .conn_thread import ConnectionThread
    from baby_modbus_tcp_server.network.datagrams import *
  File "F:/Pycharm_Projects/Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:/Pycharm_Projects/Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 10, in <module>
    from baby_modbus_tcp_server.network.processor import ModbusProcessor
  File "F:/Pycharm_Projects/Ruoyi-Energy\baby_modbus_tcp_server\network\processor.py", line 5, in <module>
    from modbus_tcp_server.data_source import BaseDataSource
ModuleNotFoundError: No module named 'modbus_tcp_server'
(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
Traceback (most recent call last):
  File ".\mdbus_server.py", line 9, in <module>
    from network.accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\__init__.py", line 1, in <module>
    from .accept_thread import ModbusTCPServer
  File "F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server\network\accept_thread.py", line 8, in <module>
    from ..data_source import BaseDataSource, TestingDataSource
ValueError: attempted relative import beyond top-level package

出现这个情况是因为python搜索module的时候是在python的类库目录下搜索,并不会搜索当前目录,就会导致各种导入问题,要解决也简单:

1.直接把文件复制到site-package下面

2.将目录加入path变量内

import sys,os
print(os.path.abspath(os.path.join(os.path.abspath(__file__),'../..')))
sys.path.append(os.path.abspath(os.path.join(os.path.abspath(__file__),'../..')))

现在就ok啦:

(venv) PS F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server> python .\mdbus_server.py
F:\Pycharm_Projects\Ruoyi-Energy\baby_modbus_tcp_server
Usage:

modbus-tcp-server <name of interface to bind to> <optional port to bind to>

    Extra allowed arguments (after the name of interface and optional port)

    0.0.0.0 will bind to all interfaces
    127.0.0.1 will bind to localhost only, which means that modbus-tcp-server will be
    reachable only from this machine

    Default port is 502

☆版权☆

* 网站名称:obaby@mars
* 网址:https://h4ck.org.cn/
* 个性:https://oba.by/
* 本文标题: 《Python 自建Module导入错误》
* 本文链接:https://h4ck.org.cn/2023/10/13816
* 短链接:https://oba.by/?p=13816
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


猜你喜欢:

4 comments

  1. Level 4
    Google Chrome 118 Google Chrome 118 Windows 10 Windows 10 cn湖北省荆州市 联通/长江大学西校区

    今天的头图很喜欢。喵喵喵

  2.  Level 3
    Google Chrome 118 Google Chrome 118 Windows 10 Windows 10 cn上海市嘉定区 电信

    之前我还学了点爬虫,还打算弄个全自动爬图片然后自动导入数据库的想法,后来才知道有图床这种东西,数据库存图片有点太大了~

    1. 公主 Queen 
      Google Chrome 116 Google Chrome 116 Mac OS X 10.15 Mac OS X 10.15 cn山东省青岛市 联通

      图片直接磁盘保存,索引可以存数据库。不然图片查看也很麻烦。嘻嘻
      自己存储的好处是完全可控啊。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注