Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Circular Import
#1
when I run the following holdem_calc.py program, I get the following error:

Error:
\Downloads\holdem_calc-1.0.0\holdem_calc>python holdem_calc.py Ad Kd Qc Qs Traceback (most recent call last): File "holdem_calc.py", line 2, in <module> from holdem_calc import holdem_functions, holdem_argparser File "C:\Users\james\Downloads\holdem_calc-1.0.0\holdem_calc\holdem_calc.py", line 2, in <module> from holdem_calc import holdem_functions, holdem_argparser ImportError: cannot import name 'holdem_functions' from partially initialized module 'holdem_calc' (most likely due to a circular import) (C:\Users\james\Downloads\holdem_calc-1.0.0\holdem_calc\holdem_calc.py)
The program that I am trying to run can be found at this link:

https://pypi.org/project/holdem-calc/

I am not sure what a circular import is. It seems to be the only way to eliminate this error. So what it it and how do I correct this error?

Any help appreciated. Thanks in advance.

Respectfully,

LZ
Reply
#2
A circular reference is when you have a group of modules that depend on each other, and the dependency path forms a closed shape (a circle). If module A imports module B and module B imports module A you have a circular dependency.

I took a quick peek at the project home page and found this at the top of holdem_calc.py
from holdem_calc import holdem_functions, holdem_argparser
So holdem_calc is importing functions from holdem_calc. It is not necessary to import functions from yourself, but the functions in this case are not in the holdem_calc.py file. My guess is this is a typo or there was a filename change or something like that and the code should be:
from holdem_functions import holdem_functions, holdem_argparser
Reply
#3
Th suggestion that you made worked. However, the are two more errors in holdem_calc.py.

import time
import holdem_functions
import holdem_argparser


def main():
    hole_cards, num, exact, board, file_name = holdem_argparser.parse_args()
    run(hole_cards, num, exact, board, file_name, True)

def calculate(board, exact, num, input_file, hole_cards, verbose):
    args = holdem_argparser.LibArgs(board, exact, num, input_file, hole_cards)
    hole_cards, n, e, board, filename = holdem_argparser.parse_lib_args(args)
    return run(hole_cards, n, e, board, filename, verbose)

def run(hole_cards, num, exact, board, file_name, verbose):
    if file_name:
        input_file = open(file_name, 'r')
        for line in input_file:
            if line is not None and len(line.strip()) == 0:
                continue
            hole_cards, board = holdem_argparser.parse_file_args(line)
            deck = holdem_functions.generate_deck(hole_cards, board)
            run_simulation(hole_cards, num, exact, board, deck, verbose)
            print "-----------------------------------"
        input_file.close()
    else:
        deck = holdem_functions.generate_deck(hole_cards, board)
        return run_simulation(hole_cards, num, exact, board, deck, verbose)

def run_simulation(hole_cards, num, exact, given_board, deck, verbose):
    num_players = len(hole_cards)
    # Create results data structures which track results of comparisons
    # 1) result_histograms: a list for each player that shows the number of
    #    times each type of poker hand (e.g. flush, straight) was gotten
    # 2) winner_list: number of times each player wins the given round
    # 3) result_list: list of the best possible poker hand for each pair of
    #    hole cards for a given board
    result_histograms, winner_list = [], [0] * (num_players + 1)
    for _ in xrange(num_players):
        result_histograms.append([0] * len(holdem_functions.hand_rankings))
    # Choose whether we're running a Monte Carlo or exhaustive simulation
    board_length = 0 if given_board is None else len(given_board)
    # When a board is given, exact calculation is much faster than Monte Carlo
    # simulation, so default to exact if a board is given
    if exact or given_board is not None:
        generate_boards = holdem_functions.generate_exhaustive_boards
    else:
        generate_boards = holdem_functions.generate_random_boards
    if (None, None) in hole_cards:
        hole_cards_list = list(hole_cards)
        unknown_index = hole_cards.index((None, None))
        for filler_hole_cards in holdem_functions.generate_hole_cards(deck):
            hole_cards_list[unknown_index] = filler_hole_cards
            deck_list = list(deck)
            deck_list.remove(filler_hole_cards[0])
            deck_list.remove(filler_hole_cards[1])
            holdem_functions.find_winner(generate_boards, tuple(deck_list),
                                         tuple(hole_cards_list), num,
                                         board_length, given_board, winner_list,
                                         result_histograms)
    else:
        holdem_functions.find_winner(generate_boards, deck, hole_cards, num,
                                     board_length, given_board, winner_list,
                                     result_histograms)
    if verbose:
        holdem_functions.print_results(hole_cards, winner_list,
                                       result_histograms)
    return holdem_functions.find_winning_percentage(winner_list)

if __name__ == '__main__':
    start = time.time()
    main()
    print "\nTime elapsed(seconds): ", time.time() - start
The first one is at line 24 in the print statement which I corrected. The second one is at line 73 in the final print statement,

which I am not sure how to fix.

Can you tell me how to fix?

Respectfully,

LZ
Reply
#4
You have somehow gotten a old Python 2 version.
Quote:(C:\Users\james\Downloads\holdem_calc-1.0.0\holdem_calc\holdem_calc.py)
pip will never install it here,so delete this as it old Python 2 version.
The right version will be in your root_python...site-packages\holdem_calc
Also after you do pip install holdem-calc.
Now it only find the old Python 2 version as you probably download it manually for some other place.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What is a circular import? asked by a newbie of newbies ineuw 1 945 Sep-16-2022, 07:27 PM
Last Post: Gribouillis
  help on pandas circular import jip31 2 8,275 May-18-2021, 11:54 AM
Last Post: jip31
  Circular import dependency hobbyist 9 3,837 Feb-23-2021, 11:57 AM
Last Post: Gribouillis
  Robot Stay Inside Circular Ring webmanoffesto 4 6,669 Dec-07-2016, 06:57 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020