Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm getting this kind of error:

Traceback (most recent call last):

  File "/Users/alex/dev/runswift/utils/sim2014/simulator.py", line 3, in <module>

    from world import World

  File "/Users/alex/dev/runswift/utils/sim2014/world.py", line 2, in <module>

    from entities.field import Field

  File "/Users/alex/dev/runswift/utils/sim2014/entities/field.py", line 2, in <module>

    from entities.goal import Goal

  File "/Users/alex/dev/runswift/utils/sim2014/entities/goal.py", line 2, in <module>

    from entities.post import Post

  File "/Users/alex/dev/runswift/utils/sim2014/entities/post.py", line 4, in <module>

    from physics import PostBody

  File "/Users/alex/dev/runswift/utils/sim2014/physics.py", line 21, in <module>

    from entities.post import Post

ImportError: cannot import name Post

also, you can see that I utilize a similar import explanation further up and it works? Is there some unwritten guideline about circular importing in? How would I utilize a similar class further down the call stack?

1 Answer

0 votes
by (26.4k points)

I think the appropriate response by jpmc26, while in no way, shape or form wrong, descends too vigorously on round imports. They can turn out great, on the off chance that you set them up accurately. 

The simplest method to do so is to utilize import my_module punctuation, instead of from my_module import some_object. The previous will quite often work, regardless of whether my_module included imports us back. The last possibly works if my_object is now characterized in my_module, which in a roundabout import may not be the situation. 

To be explicit to your case: Try evolving entities/post.py to do import physics science and afterward allude to physics.PostBody as opposed to simply PostBody straightforwardly. Essentially, change physics.py to do import entities.post and afterward use entities.post.Post as opposed to simply Post.

Wanna become a python expert? Come and Join: python course

Related questions

+3 votes
2 answers
0 votes
4 answers
asked Mar 31, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Jun 26, 2019 in Python by Anurag (33.1k points)
0 votes
1 answer
0 votes
2 answers
asked Oct 3, 2019 in Python by Tech4ever (20.3k points)

Browse Categories

...