未导入Python模块更改(Python module changes aren't being imported)

编程入门 行业动态 更新时间:2024-10-26 12:21:38
未导入Python模块更改(Python module changes aren't being imported)

我有一个python子项目引用更高目录中的模块:

/ misc/ subproject/ main2.py project/ __init__.py main1.py

要运行main2,我将cd到subproject/目录并运行

PYTHONPATH=../.. main2.py

在main2中我试图使用我写入main1的新函数,但我得到了一个

AttributeError ('module' object has no attribute 'function name').

导入后运行dir(main1)向我显示模块中没有添加任何函数; 我注释掉整个文件并尝试导入它,但是dir(main1)显示了所有旧的顶级声明,没有删除它们。 这让我觉得这是一个缓存问题,即使我已经清除了我能找到的任何*.pyc文件。

I've got a python sub-project referencing a module in a higher directory:

/ misc/ subproject/ main2.py project/ __init__.py main1.py

To run main2 I cd to the subproject/ directory and run

PYTHONPATH=../.. main2.py

In main2 I'm trying to use new functions that I've written into main1, but I'm getting an

AttributeError ('module' object has no attribute 'function name').

Running dir(main1) after the import shows me that none of the functions I added are present in the module; I commented out the entire file and tried importing it, but dir(main1) showed all the old top-level declarations, with none of them removed. This made me think it's a caching problem, even though I've cleared any *.pyc files I can find.

最满意答案

我已经安装了main1项目,所以尽管我已经将我的PYTHONPATH设置为包含当前版本的main1项目,但已安装的版本是导入的内容,因此隐藏了我对代码所做的更新。

感谢Martijn Pieters指示我使用print main1.__file__来查看main1模块导入的实际文件。

I had installed the main1 project, so even though I had set my PYTHONPATH to include the current version of the main1 project, the installed version was what was getting imported, and so shadowing the updates I was making to the code.

Thanks to Martijn Pieters for pointing me to using print main1.__file__ to see what actual file the main1 module was being imported from.

更多推荐

本文发布于:2023-08-04 06:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   Python   imported   module

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!