I have a main python script that is running another script. In the main script I have called the script and imported the function called connects and the variable connection. When I run the main script it gives me the following error when it gets to the from lh_custom import connects, connection line:
ImportError: cannot import name 'connection' from 'lh_custom'
I'm not sure why this is giving me this error when the connection variable is clearly in my lh_custom script. Anyone have an idea how to fix this?
Here is a code snippet of my main script:
import os
import stat
import filetype
import requests
import json
from lh_custom import connects, connection
from main_updated_v2 import file, filepath, prodFinishLength, brandCollectionLength, true, false, folder_name
fname = os.path.basename(file) # <-- Get the filename from the path and store in the fname variable.
print('This is filepath: ',filepath)
file_stats = os.stat(filepath)
file_size = str(file_stats.st_size)
print('The file size in bytes is: ', file_size)
print('The filename is:', file)
kind = filetype.guess(filepath)
kindoffile = kind.mime
connects()
if file.lower().endswith('.tif'):
if '_' in fname: # <-- If an underscore is in the filename
sku = fname.split('_')[0] # <-- Retrieve the item number from the file name above by taking everything before the underscore and assigning it to the sku variable
print('This is the name of the file with underscores in the filename: ',fname)
if '_' not in fname: # <-- If an underscore is not in the filename
sku = fname.split('.')[0] # < -- Retrieve the item number from the file name above by taking everything before the . and assigning it to the sku variable
print('This is the name of the file without underscores in the filename: ',fname)
c1 = connection.cursor() # <-- Initializing the cursor to the c1 variable to execute SQL statements
Here is the code for the lh_custom script:
import pyodbc
from cryptography.fernet import Fernet
def connects():
key = b'xxx=' # <-- adding the encryption of the AS400 password to the key variable
cipher_suite = Fernet(key) # <-- adding the key variable to the cipher_suite variable via the Fernet method which is part of the cryptography library
with open('AS400_password.bin', 'rb') as file_object: # <-- Retrieving the encrypted AS400 password from a stored file in a location
for line in file_object: # <-- Now that file is open do a for loop to read the encrpyted line in the file
encryptedpwd = line # <-- Assign the encryped line to the encryptedpwd variable.
uncipher_text = (cipher_suite.decrypt(encryptedpwd)) # <-- Using the .decrypt method to decrypt the encrypted password from the encryptedpwd variable and assign to the uncipher_text variable
password = bytes(uncipher_text).decode("utf-8") # <-- Decoding the decrypted password stored in the uncipher_text variable and convert to string/plain text file.
#This block of code connects to the AS400
connection = pyodbc.connect( # <-- Creating a connection to the AS400 via the pyodbc.connect method from the pyodbc library
driver='{Client Access ODBC Driver (32-bit)}', # <-- Connecting via an ODBC driver
system='10.111.11.11', # <-- Supplying the IP address for the AS400
uid='xxx', # <-- Supplying the AS400 user id
pwd=password) # <-- Supplying the decoded password stored in the password variable above to the AS400
return(connection)
connection
is only returned by connects so you should import connection with a variable instead. try this:
from lh_custom import connects
connection = connects()