0

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.

wildernessfamily
  • 405
  • 4
  • 11
  • This is likely because of how you have set up the project in PyCharm. Here PyCharm knows that it should navigate from my_program, on the other hand, when you execute it from console it tries to find your module by starting at the location of `activate.py` and of course doesn't find anything. – NotAName Dec 11 '20 at 01:29
  • @pavel okay, I understand that. To be able to share this on other server I should leave off the plugin.my_plugins and just do import messages as m? – wildernessfamily Dec 11 '20 at 01:48
  • That should work. – NotAName Dec 11 '20 at 01:53
  • I tried it but no go. In message.py I have import modules.vars as v vars is in a folder /my_program/modules/vars.py Even though I got activate.py to work messages can't find vars.py in the modules directory. :/ hmmmm Is there anything that tells Python that my_program is the root/main/src/base folder for the program? – wildernessfamily Dec 11 '20 at 02:01
  • After a lot of research I found that if I add: import sys sys.path.append('/home/my_username/My Programs/my_program') import plugins.my_plugins.messages as m to the top of activate.py It works! What is left now is that I have to figure out how to adjust this so that the path adjust to individual servers who use this code. Instead of having to enter it manually. – wildernessfamily Dec 11 '20 at 03:03

1 Answers1

0

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.

wildernessfamily
  • 405
  • 4
  • 11