#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
print(__file__)
print(os.path.realpath(__file__))
print('using sys.executable:', repr(os.path.dirname(os.path.realpath(sys.executable))))
print('using sys.argv[0]:', repr(os.path.dirname(os.path.realpath(sys.argv[0]))))
print(sys.argv[0])
print(sys.path[0])
print(os.getcwd())
input("请输入任意键继续")
↑↑↑以上为测试代码↑↑↑
源码路径为D:\TestCode\testPath.py
exe文件路径为D:\TestCode\dist\testPath.exe
命令行进入源码目录下运行
命令:
D:\TestCode>python testPath.py
结果:
testPath.py
D:\TestCode\testPath.py
using sys.executable: 'C:\\Users\\LQT\\AppData\\Local\\Programs\\Python\\Python37'
using sys.argv[0]: 'D:\\TestCode'
testPath.py
D:\TestCode
D:\TestCode
命令行进入非源码目录下运行
命令:
E