glotchimo
glotchimo

Reputation: 685

Function call and file path confusion

I have two scripts, main and statistics. In one script, main, several functions from the other script, statistics, are called. The code is as follows.

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

Of course, the functions match as far as calling names go, and my IDE (PyCharm) is not giving any warnings. Tests have, refactor after refactor, turned up the fundamentally same error report:

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'

There are, however, a few intriguing recurrences that have popped up. First of all, in some cases, for no identifiable reason, the tests check out when the function names are changed, however, the results are not consistent with further tests. Second of all, the use of back/forward slashes in the file paths are not consistent, with a G:/ with the first call, and then G:\ for the two latest calls, though the first and latest calls are referring to the same file.

My question is whether this error is a matter of function naming, a matter of file path inconsistencies, a matter of cross-imports, or due to the fact that the functions being called are not housed in a class.

Project Structure:

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

Import Structure:

# in main.py
import statistics

# in statistics.py
import main

statistics.py Structure:

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

Upvotes: 0

Views: 82

Answers (1)

Dewald Abrie
Dewald Abrie

Reputation: 1482

I'll bet it's because of the cross-import. Try to move anything that statistics.py require to that file and then import into and call from main.py.

Your function naming is normal.

I wouldn't worry about the mix of slashes. However, if you're constructing a path yourself, use the os module:

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

This will ensure that you get a path suited to your platform.

Upvotes: 1

Related Questions