Python project deployment on Ubuntu 18 server

27 Views Asked by At

Ive created my simple script (tg bot), but after uploading on my Ubuntu server (python in alternative mode ==3.9.13, pip freeze and installed requirements). There is some path (I suppose) problem:

root@tele:/# python3 /root/mybot/workDir/coreFiles/coreBot.py
Traceback (most recent call last):
  File "/root/mybot/workDir/coreFiles/coreBot.py", line 6, in <module>
    from workDir.sets import config
ModuleNotFoundError: No module named 'workDir'

In my core file there is some imports:

from workDir.sets import config
from workDir.methods.states.comdsHandler import getHandler

Here is my project tree:

workDir/
    ├─coreFiles/
    │ └─coreBot.py
    ├─methods/
    │ ├─states/
    │ │ ├─actionStates.py
    │ │ └─comdsHandler.py
    │ └─utils/
    │   ├─connectionDB.py
    │   ├─loggingUtil.py
    │   ├─registerUserInDB.py
    │   └─strParser.py
    └─sets/
      ├─categories.py
      └─config.py

Kindly help find better solution than make it in one py file :)

1

There are 1 best solutions below

1
pb. On

There is a couple issues. You are handling folders as if they are python files and you may be getting confused in the folder structure. To clarify:

import X from Y --> would return X (which could be a class or function or whatever) within the file Y.py

Hence, in your case you want to just:

import workDir.sets.config

As you aren't loading anything specific from config.py, you just want the whole file.

Additionally, the import structure is heavily dependent on where your working directory for the script you are running is within. Is the script you are running inside workDir? If so, you don't need to put import workDir.sets.config import .sets.config should suffice.

If you don't know the directory run print(os.getcwd())

EDIT: I noticed you are loading from /workDir/coreFiles/coreBot.py, is this correct? If so, you need to load a file from a parent directory:

import ..sets.config