简体   繁体   中英

Python3.9 ModuleNotFoundError: No module named Can't figure out base directory

I'm running into a problem with a program I'm creating and I can't figure out why I'm receiveing this error. I'm sure it something simple and I've just been working on it to long and my eyes need a rest.

My directory layout is this:

/home/my_username/My Programs/my_program
my_program
├── __init__.py
├── plugins
├────└── __init__.py
├──────└my_plugins
├─────────└── __init__.py
├─────────└── activate.py
├─────────└── messages.py

Here is my code in messages.py
-----messages.py-----

welcome_msg = "Hello, thanks for trying my program."

Here is my code in activate.py
-----activate.py-----

import plugins.my_plugins.messages as m
    
print(m.welcome_msg)

I'm using PyCharm and the program works just fine in PyCharm and show no errors.

Outside of PyCharm when I run:

python3.9 activate.py

I receive this error
ModuleNotFoundError: No module named 'plugins'

If I change activate.py to look like this, it works in the terminal outside of PyCharm but then inside PyCharm I receive an error "No module named messages" BUT. when I run it in PyCharm with the error it works. It picks up messages.py module and displays the correct text.
-----activate.py-----

import plugins.my_plugins.messages as m
    
print(m.welcome_msg)

I'm self teaching my self Python. Is there some kind of snippet that tells Python where the base directory is. In this example that my_program is the base directory? Or should I just ignore PyCharm error? I've never ignored an error that PyCharm has given so that would be a first.

Thanks in advance for your time in helping me with this problem.

Ps. Please excuse any spelling or grammar mistakes I was born with severe dyslexia.

I researched StackOverFlow before posting this question and didn't find this q/a. This morning I started researching again and found a deatiled answer on how to solve this problem.
Relative imports in Python 3

I choose option 2 which solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM