File "test/test_plc.py", line 5, in TestPLC from ..scripts.modules.tcp import ftpServer ImportError: attempted relative import with no known parent package
时间: 2023-06-30 16:24:12 浏览: 55
这个错误提示说明你尝试使用相对导入,但是Python无法确定相对导入的起点。
相对导入是基于当前模块的位置,而不是当前工作目录。因此,当你在一个没有包结构的脚本中使用相对导入时,Python会无法确定相对导入的起点。
解决这个问题的方法是将脚本转换成包结构,即将脚本所在的目录变成一个包,并在该包中使用相对导入。具体地,你可以按照以下步骤进行操作:
1. 在脚本所在的目录上一级目录下创建一个名为`__init__.py`的空文件,这个文件是包的标识文件,必须存在。
2. 将脚本文件移动到该目录下,并将脚本文件名改为`__main__.py`,这是Python解释器默认执行的脚本文件名。
3. 在`__main__.py`中使用相对导入,例如:
```python
from .scripts.modules.tcp import ftpServer
```
这里的`.`表示当前包,也就是`__main__.py`所在的包。
4. 在该目录下执行命令`python -m <包名>`,即可运行你的脚本。
注意,这里的`<包名>`指的是你创建的包的名称,即包含`__main__.py`和`__init__.py`的目录名。
希望这个解答对你有帮助,如果你还有其他问题,可以继续问我。
相关问题
from ..scripts.modules.tcp import ftpServer ImportError: attempted relative import with no known parent package
这个错误提示说明你尝试使用相对导入,但是Python无法确定相对导入的起点。
相对导入是基于当前模块的位置,而不是当前工作目录。因此,当你在一个没有包结构的脚本中使用相对导入时,Python会无法确定相对导入的起点。
解决这个问题的方法是将脚本转换成包结构,即将脚本所在的目录变成一个包,并在该包中使用相对导入。具体地,你可以按照以下步骤进行操作:
1. 在脚本所在的目录下创建一个名为`__init__.py`的空文件,这个文件是包的标识文件,必须存在。
2. 将脚本文件移动到该目录下,并将脚本文件名改为`__main__.py`,这是Python解释器默认执行的脚本文件名。
3. 在`__main__.py`中使用相对导入,例如:
```python
from .scripts.modules.tcp import ftpServer
```
这里的`.`表示当前包,也就是`__main__.py`所在的包。
4. 在该目录下执行命令`python -m <包名>`,即可运行你的脚本。
注意,这里的`<包名>`指的是你创建的包的名称,即包含`__main__.py`和`__init__.py`的目录名。
希望这个解答对你有帮助,如果你还有其他问题,可以继续问我。
/home/nvidia/cotton_ws/src/wirecontrol/scripts/wired_control.py: line 3: import: command not found
引用\[1\]:报错(1):/bin/sh: 1: flex: not found。解决方案(1):使用命令sudo apt-get install flex来安装flex。引用\[2\]:报错:/bin/sh: 1: flex: not found。原因:缺少库。解决:使用命令sudo apt-get install flex来安装flex。引用\[3\]:报错:/bin/sh: 1: flex: not found。解决方案:使用命令sudo apt-get install flex来安装flex。问题:/home/nvidia/cotton_ws/src/wirecontrol/scripts/wired_control.py: line 3: import: command not found。
回答: 这些报错信息表明在配置内核菜单或运行脚本时缺少了flex库。为了解决这个问题,你可以使用命令sudo apt-get install flex来安装flex库。这个命令会自动下载并安装所需的库文件。安装完成后,你应该能够成功运行配置菜单或脚本。
#### 引用[.reference_title]
- *1* [make menuconfig错误: /bin/sh: 1: flex: not found 和 /bin/sh: 1: bison: not found](https://blog.csdn.net/qq_36393978/article/details/118225531)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [linux报错:/bin/sh: 1: flex: not found scripts/Makefile.host:9: recipe for target ‘scripts/kconfig/...](https://blog.csdn.net/u010835747/article/details/118282135)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [【错误记录】编译 Linux 内核报错 ( /bin/sh: 1: flex: not found )](https://blog.csdn.net/han1202012/article/details/123656271)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)