python Error: import module not found after making executable

python Error: import module not found after making executable

Error image

from pathlib import Path
import linecache
import pyperclip
print('Looking for a path...')
print('Found path!')
Path('C:/Users/Akush/Documents/Warcraft III/CustomMapData/YouTD/')
a = linecache.getline('savecode.txt',7)
pyperclip.copy(a)
print('{} copied to clipboard!'.format(a))

所以在 pycharm 中一切正常,但是当我从 .py 生成 .exe 时,它​​在 CMD

中出现 "Module not found" 错误

你知道我做错了什么吗? 感谢您的帮助!

由于您使用的是 pycharm,请确保您在项目设置中使用了正确的 python 解释器。如果您使用的是系统解释器,则不会在虚拟环境中找到这些模块

我稍微清理了你的代码:

import os
from pathlib import Path
import linecache
import pyperclip

# Specify the path
dir = Path('C:/Users/Akush/Documents/Warcraft III/CustomMapData/YouTD/')
# Specify the file
file = 'savecode.txt'

# Start Searching for Path
print('Looking for a path...')

# Check if Path exists
if dir.is_dir():
    # Set the currect working directory to the found path
    os.chdir(dir)
    # Let the user know the path has been found
    print('Found path!')
    # Check to see if the file exists
    if Path(file).is_file():
        # Get lines from file
        a = linecache.getline(file, 7)
        if a == '':
            print('Nothing found in file')
        else:
            # Copy line to clipboard
            pyperclip.copy(a)
            print(f'{a} copied to clipboard!')
    else:
        print("File not found")
else:
    print('This directory does not exist')