Home > Blockchain >  From Import in Python is not importing my modules
From Import in Python is not importing my modules

Time:10-26

I have several custom module scrips in my python project that are all in a folder that is one directory down named "modules". After about a day of use on every machine I touch, importing these modules starts spitting out this error:

ImportError: cannot import name 'letters' from 'modules' (C:\Program Files\Python39\lib\site-packages\modules.py)

The code for importing custom modules is here.

    from modules import letters
    from modules import sfx
    from modules import bgm
    from modules.colors import colors, hex2dec
    from modules import lessons
    from modules import sessions
    from key import key

The project folder looks like this (folders and files unrelated to the issue excluded):

formapp
├───modules
│   ├───bgm.py
│   ├───bot_ir.py
│   ├───chat.py
│   ├───colors.py
│   ├───formbot.py
│   ├───ir.py
│   ├───lessons.py
│   ├───letters.py
│   ├───sessions.py
│   └───sfx.py
├───app.py
├───key.py

For the most part, sys.path.append(r"modules") had worked to solve this issue however hex2dec in colors.py still caused errors. Regardless, of the 15 people working on this project this error only appears for me so I should not have to append a new folder to search for modules. The only thing that separates me from the other 15 people is that I was using formbot.py

Here is the code for formbot.py.

import requests
import time

class FormBot():
    def __init__(self, username, password, timeout=0, host='127.0.0.1', port=420)       :
        self.host = '192.168.10.69'
        self.port = 420
        self.username = username
        self.password = password
        self.loggedin = False
        time.sleep(timeout)
        self.login()
        print('logged in as', username)


    #change "userType": "login", to "usertype": "new" on first time use
    def login(self):
        loginAttempt = requests.post(url="http://" self.host ":" str(self.port) "/login", data={"username": self.username, "password": self.password, "userType": "new", "bot": "True", "forward":"/"})



#forbius login

Forbius = FormBot('Forbius', 'Password#1')

I have also tried reinstalling Python entirely and that has not worked.

To test, I tried running a fresh copy of the main branch on a computer affected by this issue and got the same error. The same code works fine on other machines.

CodePudding user response:

You'll need to add a file called __init__.py to your modules folder. The file can be empty. Review this SO thread for more details: What is __init__.py for?

CodePudding user response:

try adding an __init__.py file inside the modules folder

inside the __init__.py file put:

from .letters import *
from .sfx import *
from .bgm import *
.
.
.
  • Related