I'm new to python and I don't know how to organize the project structure in the right way, so all auto imports would work in pycharm.
That's my current structure.
In PublisherSubscriberTest pycharm generated this import
from Rabbit.RabbitReceiver import RabbitReceiver
from Rabbit.RabbitSender import RabbitSender
But it's not working. That's the output.
ImportError: No module named Rabbit.RabbitReceiver
What have I done wrong?
I'm more familiar with java. And for example in java I would just create package with some classes and then I would be able to import them anywhere in my project. AFAIK it's not the same with python somehow.
Could someone explain this to me?
EDIT1: Yes, I know about sys.path.append. I used to do it that way, but It seemed strange to me and i want to be able to do it without it.
Structure tool window. View | Tool Windows | Structure or Alt+7. This tool window displays the structure of a file currently opened in the editor and having the focus, or selected in the Project tool window. For diagrams, this tool window shows the diagram preview.
Whatever you do in PyCharm, you do that in the context of a project. A project is an organizational unit that represents a complete software solution. It serves as a basis for coding assistance, bulk refactoring, coding style consistency, and so on. Once you have opened a folder in PyCharm, the .
To configure project settings, select PyCharm | Preferences for macOS ( Ctrl+Alt+S ) or File | Settings for Windows and Linux. icon apply only to the current project. Other settings are global and apply to all existing projects.
import sys, os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from Rabbit.RabbitReceiver import RabbitReceiver
from Rabbit.RabbitSender import RabbitSender
If you don't want to modify sys.path
, the only way is to add -m
flag when you run it
python -m messaging_system.tests.PublisherSubscriberTest
see How to fix "Attempted relative import in non-package" even with __init__.py
OK, finally I found an ultimate answer: Relative imports for the billionth time
I suggest you read that post carefully, from which I learned a lot.
In short, if you want to do this, you have to add path-to-Rabbit
to sys.path
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With