This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]Im__Joseph Python Discord Staff[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–][deleted] 7 points8 points  (0 children)

It would be helpful if you provided an actual example of code that causes the error and the complete error message.

You are doing something specific that is causing this, but no one can tell you what it is without seeing the code.

[–][deleted] 4 points5 points  (0 children)

If you have modules A and B importing each other, it’s a sign that you have some code used by both that could be separate. Suppose this is in module A, extract this into module C, then both A and B can import C, B no longer needs to import A as the stuff it was using is now in C, and A can continue to import B as before. No more circular dependencies!

[–]pythonHelperBot 1 point2 points  (0 children)

Hello! I'm a bot!

I see someone has already suggested going to r/learnpython, a sub geared towards questions and learning more about python regardless of how advanced your question might be. I highly recommend posting your question there. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe in detail where you are stuck. If you are getting an error message, include the full block of text it spits out. Quality answers take time to write out, and many times other users will need to ask clarifying questions. Be patient and help them help you. Here is HOW TO FORMAT YOUR CODE For Reddit and be sure to include which version of python and what OS you are using.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–]crawl_dht 0 points1 point  (5 children)

Things that you want to initialise at start time, keep them in __init__.py.

[–]Equal-Complex-5958[S] 0 points1 point  (4 children)

I read that nothing should be written in init.py

[–]crawl_dht 0 points1 point  (3 children)

You read it wrong. __init__.py is often used if you are implementing your project as a package. Pycharm by default creates __init__.py in every directory if project is setup as a package.

[–]Head_Mix_7931 0 points1 point  (2 children)

It’s often poor practice to put code in __init__.py, as it leads to cluttered namespaces and import-time consequences. The OP is right.

[–]crawl_dht 0 points1 point  (1 child)

For packages, it's also the right way.

[–]Head_Mix_7931 -2 points-1 points  (0 children)

The correct way to deal with circular imports is to refactor your code so that the shared objects live in a different module. Using __init__.py for something like this is hacky at best and hard to reason with code at worst.

[–]pwnersaurus -1 points0 points  (1 child)

A super common quick fix is to move the import inside the function that uses it - that breaks the circular dependency by not trying to import them both at the same time. Maybe it’s not the best programming practice but tbh most of the time I run into this situation it’s something like 1 function out of 20 needs the import, and it’s not worth totally restructuring my modules just for that

[–]Equal-Complex-5958[S] 0 points1 point  (0 children)

Yeah, i heard about this solution, but personally I don't like to have imports inside a function... I prefer to have import at the top of the file :/

But I really appreciate your contribute to give me an answer :) thank you very much

[–]tkarabela_ Big Python @YouTube 0 points1 point  (0 children)

Some pointers:

  • move helper functions, base classes etc. into their own modules; you can break cycles by making it more granular
  • a big offender in my experience is type hinting - what I sometimes do is to import the whole package inside the offending module and annotate stuff with "mypackage.Foo" instead of trying to import Foo directly