0

I am very new to Python and I have the following structure for a project:

    server/
        ├── config/
        │   ├── __init__.py
        │   ├── application.py
        │   ├── dev.py
        │   └── qa.py
        ├── lib/
        │   ├── __init__.py
        │   ├── redisdb.py
        │   ├── logger.py
        │   └── geo.py
        └── scripts/
            ├── __init__.py
            ├── my_first_script.py
            └── my_second_script.py

and in my_first_script.py file, I have the following code:

import pickle
from lib.redisdb import r
import re
import config.application as appconf

print( appconf.DOCUMENT_ENDPOINT )

partnerData = pickle.loads(r.get("partner_date_all"))

print( len(partnerData) )

When I run this code in the terminal using the command

python server/scripts/my_first_script.py

I am getting the following error:

Traceback (most recent call last):
  File "my_first_script.py", line 3, in <module>
    from lib.redisdb import r
ImportError: No module named lib.redisdb

I am using Python 2.7 version here. When I checked with Python 3 also, I have the same error. Now how can I execute this file? If my code doesn't have imports from the other local modules, everything works just fine.

Happy Coder
  • 4,255
  • 13
  • 75
  • 152
  • Does this answer your question? [Python error "ImportError: No module named"](https://stackoverflow.com/questions/338768/python-error-importerror-no-module-named) – Harun Yilmaz Mar 30 '22 at 15:57
  • You can check this answer if the issue is not virtual environment: https://stackoverflow.com/a/23210066/1331040 – Harun Yilmaz Mar 30 '22 at 15:58

1 Answers1

0

Your modules are all siblings and you didn't not declare a parent package.

You could modify your structure this way so your modules can know each other.

server/ (assuming this is your project root)
    server/ (assuming you want to call your package "server")
        ├── __init__.py
        ├── server.py (your package entry point)
        ├── config/
        │   ├── __init__.py
        │   ├── application.py
        │   ├── dev.py
        │   └── qa.py
        ├── lib/
        │   ├── __init__.py
        │   ├── redisdb.py
        │   ├── logger.py
        │   └── geo.py
        └── scripts/
            ├── __init__.py
            ├── my_first_script.py
            └── my_second_script.py

And now your imports can refer to the parent package:

for example:

# my_first_script.py
from server.config import application
Peterrabbit
  • 2,166
  • 1
  • 3
  • 19
  • That is correct , but in the server there is a corn job configured with this command `/usr/local/bin/python3.6 /path/server/scripts/update_partnerdata.py` and this seems to be working before. Now when I try to execute this command itself in terminal in server, it is showing same error there too. – Happy Coder Mar 30 '22 at 16:22
  • Maybe you have to adapt the way you import the modules.. it's hard to say without seeing the actual files and code... Anyway the rules to make a module visible from another are pretty much always the same. – Peterrabbit Mar 30 '22 at 17:25