当 运行 我的 Python 脚本时,如何修复导入错误?
How can I fix an Import Error when running my Python script?
我有一个主要的 python 脚本,它是 运行 另一个脚本。在主脚本中,我调用了脚本并导入了名为 connects 的函数和变量 connection。当我 运行 主脚本到达 from lh_custom import connects, connection line:
时出现以下错误
ImportError: cannot import name 'connection' from 'lh_custom'
我不确定为什么当连接变量显然在我的 lh_custom 脚本中时会出现此错误。有人知道如何解决这个问题吗?
这是我的主要脚本的代码片段:
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
这是 lh_custom 脚本的代码:
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
仅由连接返回,因此您应该使用变量导入连接。试试这个:
from lh_custom import connects
connection = connects()
我有一个主要的 python 脚本,它是 运行 另一个脚本。在主脚本中,我调用了脚本并导入了名为 connects 的函数和变量 connection。当我 运行 主脚本到达 from lh_custom import connects, connection line:
时出现以下错误ImportError: cannot import name 'connection' from 'lh_custom'
我不确定为什么当连接变量显然在我的 lh_custom 脚本中时会出现此错误。有人知道如何解决这个问题吗?
这是我的主要脚本的代码片段:
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
这是 lh_custom 脚本的代码:
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
仅由连接返回,因此您应该使用变量导入连接。试试这个:
from lh_custom import connects
connection = connects()