You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spyder中自定义模块导入报错:NameError: name 'rotor' is not defined

Troubleshooting NameError: 'rotor' is not defined When Importing Custom Module in Spyder

Let's walk through the most likely fixes for your issue, since this cropped up right after your Anaconda reinstall:

1. Fix Your Import Syntax

The most common culprit here is how you're referencing the rotor function after importing the module. When you use import rot, Python loads the entire module into the rot namespace—so you can't call rotor() directly. You have two simple fixes:

  • Call the function using the module prefix:
    f = rot.rotor(b_x, b_y, b_z, x, y, z)
    
  • Or adjust your import statement to pull the function directly into your main script's namespace:
    from rot import rotor
    # Now you can call it as:
    f = rotor(b_x, b_y, b_z, x, y, z)
    

2. Verify File Structure & Python Path

First, confirm rot.py and main.py are in the same directory (C:\Users\Professional\.spyder-py3\my_pro\jgut). If they're not, you need to tell Python where to find rot.py. Add this at the top of main.py to check and fix the path:

import sys
# Print all paths Python checks for modules
print(sys.path)
# Add the directory containing rot.py if it's missing
sys.path.append("C:/Users/Professional/.spyder-py3/my_pro/jgut")
import rot

3. Adjust Spyder's Module Reload Settings

Anaconda resets Spyder's preferences during reinstall, which probably broke the "force reload" behavior you used before:

  • Go to Tools > Preferences > Python interpreter
  • Under the User module reload section, ensure "Enable automatic reloading of modules" is checked
  • If automatic reload doesn't work, manually reload the module in your script:
    import importlib
    import rot
    importlib.reload(rot)
    
  • You can also try running your script in an external terminal (go to Run > Configuration per file and check "Execute in an external system terminal")—this bypasses Spyder's internal namespace caching quirks.

4. Check for Hidden Function in rot.py

Double-check your rot.py code to make sure the rotor function isn't trapped inside a conditional block like if __name__ == "__main__":. For example, this would make the function unimportable:

# Bad: Function only works when running rot.py directly
if __name__ == "__main__":
    def rotor(bx,by,bz,x,y,z):
        import numpy as np
        # Your code here

Move the rotor function definition outside of this block so it's visible when importing the module.

5. Clear Spyder's Cache

Spyder might be clinging to old, cached versions of your module. Close Spyder completely, delete the cache files in C:\Users\Professional\.spyder-py3\ (focus on folders like temp or spyder-py3), then restart Spyder and run your script again.

内容的提问来源于stack exchange,提问作者Иван Вовч

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 22:24:04