ModuleNotFoundError: No module named 'compiler.ast'
时间: 2023-10-18 08:25:29 浏览: 83
根据引用和引用的内容,你遇到的报错"ModuleNotFoundError: No module named 'compiler.ast'"是因为Python 3中已废除了compiler模块。在Python 3中,你需要使用其他方法来实现相同的功能。
根据引用中提供的解决方法,你可以使用以下代码来替代在Python 3中废除的compiler模块:
```python
import collections
def flatten(x):
result = []
for el in x:
if isinstance(el, collections.Iterable) and not isinstance(el, str):
result.extend(flatten(el))
else:
result.append(el)
return result
print(flatten(["junk",["nested stuff"],[],[[]]]))
```
这个代码将递归地将嵌套的列表展平为一个一维列表。你可以将你的列表作为参数传递给`flatten`函数,并打印结果。
如果你在Python 2中使用了`from compiler.ast import flatten`,可以参考引用中提供的代码,在Python 3中实现相同的功能。
希望这能帮到你!如果还有其他问题,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [ModuleNotFoundError: No module named 'compiler'](https://blog.csdn.net/chenhaojing/article/details/82829611)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [python展开list嵌套,并解决from compiler.ast import flatten报错ModuleNotFoundError: No module named ...](https://blog.csdn.net/weixin_35757704/article/details/120110267)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pack](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)