函数调用和文件路径混淆

Function call and file path confusion

我有两个脚本,mainstatistics。在一个脚本 main 中,调用了另一个脚本 statistics 中的多个函数。代码如下

if initial_action == "get_portfolio_statistics":
    statistics.get_portfolio_status()
    statistics.get_current_securities()
    statistics.get_open_trades()

当然,就调用名称而言,函数是匹配的,我的 IDE (PyCharm) 没有给出任何警告。经过测试,重构后的重构,出现了基本相同的错误报告:

Traceback (most recent call last):
  File "G:/stuff/dev/!projects+repositories/RYZ/main.py", line 2, in <module>
    import statistics
  File "G:\stuff\dev\!projects+repositories\RYZ\statistics.py", line 1, in 
   <module>
    import main
  File "G:\stuff\dev\!projects+repositories\RYZ\main.py", line 12, in <module>
    statistics.get_portfolio_status()
AttributeError: module 'statistics' has no attribute 'get_portfolio_status'

然而,有一些有趣的重复出现了。首先,在某些情况下,由于无法识别的原因,测试会在函数名称更改时检查出来,但是结果与进一步测试不一致。其次,在文件路径中使用 back/forward 斜杠不一致,第一次调用使用 G:/,最近两次调用使用 G:\,虽然第一次调用和最近的调用指的是同一个文件。

我的问题是这个错误是函数命名的问题、文件路径不一致的问题、交叉导入的问题,还是因为被调用的函数没有包含在 class.

项目结构:

ryz server backend (RYZ directory)
 - main.py
 - statistics.py

导入结构:

# in main.py
import statistics

# in statistics.py
import main

statistics.py结构:

<imports...>
def get_portfolio_status():
    <code>
def get_current_securities():
    <code>
def get_open_trades():
    <code>

我敢打赌这是因为 cross-import。尝试将 statistics.py 需要的任何内容移动到该文件,然后导入并从 main.py.

调用

你的函数命名是正常的。

我不担心斜线的混合。但是,如果您自己构建路径,请使用 os 模块:

import os
path = os.path.join(os.getcwd(), 'dir', 'filename')

这将确保您获得适合您平台的路径。