Python代码未在可执行文件中执行所有代码行。

huangapple go评论101阅读模式
英文:

python code does not execute all lines of code in exe file

问题

I have used pyinstaller to convert it but the code does not execute all my lines code in the exe file and cmd, but to notice is the original lines code are running perfectly in vs code

我已经使用pyinstaller来转换它,但该代码在exe文件和cmd中无法执行我的所有代码行,但值得注意的是原始代码在vs code中运行得很好。

I tried py installer also another named "auto py to exe"
我尝试了py installer还有另一个名为"auto py to exe"

The only thing I want to make the code run in user's windows
我唯一想要的是让代码在用户的Windows上运行

英文:

I have used pyinstaller to convert it but the code does not execute all my lines code in the exe file and cmd , but to notice is the original lines code are running perfectly in vs code

i tried py installer also another named "auto py to exe "
the only thing I want to make the code run in user's windows

  1. import datetime
  2. from dateutil.relativedelta import relativedelta
  3. print("Welcome to the Recharge Assistant")
  4. # Get today's date
  5. today = datetime.date.today()
  6. # Get the same day next year
  7. same_day_next_year = datetime.date(today.year + 1, today.month, today.day)
  8. # Get a valid date from the user
  9. while True:
  10. user_input = input("Please enter your current validity (DD/MM/YYYY), or 'q' to quit: ")
  11. # Check if user wants to quit
  12. if user_input.lower() == 'q':
  13. break
  14. try:
  15. # Convert user's input to a date object
  16. user_date = datetime.datetime.strptime(user_input, "%d/%m/%Y").date()
  17. # Check if user's input is in the past or exceeds the same day next year
  18. if user_date < today:
  19. user_date = today
  20. elif user_date > same_day_next_year:
  21. user_date = same_day_next_year
  22. # Calculate the number of days between user_date and same_day_next_year
  23. DAYS_DIFF = (same_day_next_year - user_date).days
  24. if DAYS_DIFF == 0:
  25. print("You may have entered a wrong year or date!")
  26. # Handle leap year
  27. if DAYS_DIFF == 366:
  28. DAYS_DIFF = 365
  29. # Calculate the number of months and days in days_diff
  30. months = DAYS_DIFF // 30
  31. days = DAYS_DIFF % 30
  32. # Print the updated user date
  33. print("Current user Validity:", user_date)
  34. print("Max number of days that can be recharged until next year:", DAYS_DIFF)
  35. print(f"Number of months that can be recharged: {months} months and {days} days")
  36. while True:
  37. try:
  38. get_month = int(input("How many months do you need? "))
  39. get_days = get_month * 30
  40. new_date = user_date + relativedelta(months=get_month)
  41. print(f"You entered {get_month} months which is {get_days} days.")
  42. print("The resulting date will be:", new_date)
  43. break
  44. except ValueError:
  45. print("Invalid input. Please enter a valid integer.")
  46. except ValueError:
  47. print("Invalid date format. Please enter a valid date in the format DD/MM/YYYY.")
  48. # Add a break statement if the user entered a valid date to allow for quitting
  49. if user_input.lower() != 'q':
  50. break

答案1

得分: 1

从未遇到过这个问题,但你应该尝试重新安装Py,并检查是否缺少可能引起此错误的依赖项。

我建议运行 $ pip check,即使你是否在使用它,也要检查你的Conda环境。

我研究了你的情况,听说你可以尝试关闭控制台,或者将控制台设置为 false

英文:

Never had this problem before, but you should try reinstalling Py and Check if you have missing dependencies that can cause this error.

I suggest $ pip check and even if youre using it or not check your Conda eviroment.

I study your case and hear about you can try to shutdown the console or either set console ==true to false

答案2

得分: 1

经过再次查看,我意识到问题不是可执行文件的创建,而是异常处理的方式。我认为最好让您具体考虑在哪些行上您希望捕获异常。在这里,我将user_date = datetime.datetime.strptime(user_input, "%d/%m/%Y").date()这一行用try-except块单独包装起来,以便其他地方捕获的异常不会影响到这里。我还添加了一个continue关键字,使其在循环内继续执行。程序在完成后将立即关闭,因此您可以删除最后的if语句,让用户使用"q"作为输入来退出,或者添加一个sleep语句,以便用户有时间查看他们的结果。
我使用pyinstaller filename.py --onefile构建了这个程序,经过测试,现在应该可以正常工作。

  1. import datetime
  2. from dateutil.relativedelta import relativedelta
  3. print("欢迎使用充值助手")
  4. # 获取今天的日期
  5. today = datetime.date.today()
  6. # 获取明年的同一天
  7. same_day_next_year = datetime.date(today.year + 1, today.month, today.day)
  8. # 从用户那里获取有效日期
  9. while True:
  10. user_input = input("请输入您当前的有效期(DD/MM/YYYY),或输入'q'退出:")
  11. # 检查用户是否想要退出
  12. if user_input.lower() == 'q':
  13. break
  14. try:
  15. # 将用户输入转换为日期对象
  16. user_date = datetime.datetime.strptime(user_input, "%d/%m/%Y").date()
  17. except ValueError:
  18. print("日期格式无效。请输入格式为DD/MM/YYYY的有效日期。")
  19. continue
  20. # 检查用户输入是否在过去或超过明年的同一天
  21. if user_date < today:
  22. user_date = today
  23. elif user_date > same_day_next_year:
  24. user_date = same_day_next_year
  25. # 计算user_date和明年同一天之间的天数差异
  26. DAYS_DIFF = (same_day_next_year - user_date).days
  27. if DAYS_DIFF == 0:
  28. print("您可能输入了错误的年份或日期!")
  29. # 处理闰年
  30. if DAYS_DIFF == 366:
  31. DAYS_DIFF = 365
  32. # 计算days_diff中的月数和天数
  33. months = DAYS_DIFF // 30
  34. days = DAYS_DIFF % 30
  35. # 打印更新后的用户日期
  36. print("当前用户有效期:", user_date)
  37. print("直到明年最多可以充值的天数:", DAYS_DIFF)
  38. print(f"可以充值的月数:{months}个月{days}天")
  39. while True:
  40. try:
  41. get_month = int(input("您需要多少个月?"))
  42. get_days = get_month * 30
  43. new_date = user_date + relativedelta(months=get_month)
  44. print(f"您输入了{get_month}个月,即{get_days}天。")
  45. print("结果日期将为:", new_date)
  46. break
  47. except ValueError:
  48. print("无效输入。请输入有效的整数。")
英文:

After taking another look at this I realized that the problem wasn't the creation of the executable but it was instead how the exceptions were handled. I think that is would be better for you to consider specifically what lines you are expecting to capture an exception at. Here I wrapped the line user_date = datetime.datetime.strptime(user_input, &quot;%d/%m/%Y&quot;).date() with the try-except block on its own, so other exceptions being caught elsewhere would not cause this to capture them. I also added a continue keyword to make it continue inside the loop. The program will close immediately when finished, so you should either remove the last if-statement and have the user quit out with "q" as an input, or add a sleep statement to give the user time to see their results.
I built this with pyinstaller filename.py --onefile tested it, and it should be working now.

  1. import datetime
  2. from dateutil.relativedelta import relativedelta
  3. print(&quot;Welcome to the Recharge Assistant&quot;)
  4. # Get today&#39;s date
  5. today = datetime.date.today()
  6. # Get the same day next year
  7. same_day_next_year = datetime.date(today.year + 1, today.month, today.day)
  8. # Get a valid date from the user
  9. while True:
  10. user_input = input(&quot;Please enter your current validity (DD/MM/YYYY), or &#39;q&#39; to quit: &quot;)
  11. # Check if user wants to quit
  12. if user_input.lower() == &#39;q&#39;:
  13. break
  14. try:
  15. # Convert user&#39;s input to a date object
  16. user_date = datetime.datetime.strptime(user_input, &quot;%d/%m/%Y&quot;).date()
  17. except ValueError:
  18. print(&quot;Invalid date format. Please enter a valid date in the format DD/MM/YYYY.&quot;)
  19. continue
  20. # Check if user&#39;s input is in the past or exceeds the same day next year
  21. if user_date &lt; today:
  22. user_date = today
  23. elif user_date &gt; same_day_next_year:
  24. user_date = same_day_next_year
  25. # Calculate the number of days between user_date and same_day_next_year
  26. DAYS_DIFF = (same_day_next_year - user_date).days
  27. if DAYS_DIFF == 0:
  28. print(&quot;You may have entered a wrong year or date!&quot;)
  29. # Handle leap year
  30. if DAYS_DIFF == 366:
  31. DAYS_DIFF = 365
  32. # Calculate the number of months and days in days_diff
  33. months = DAYS_DIFF // 30
  34. days = DAYS_DIFF % 30
  35. # Print the updated user date
  36. print(&quot;Current user Validity:&quot;, user_date)
  37. print(&quot;Max number of days that can be recharged until next year:&quot;, DAYS_DIFF)
  38. print(f&quot;Number of months that can be recharged: {months} months and {days} days&quot;)
  39. while True:
  40. try:
  41. get_month = int(input(&quot;How many months do you need? &quot;))
  42. get_days = get_month * 30
  43. new_date = user_date + relativedelta(months=get_month)
  44. print(f&quot;You entered {get_month} months which is {get_days} days.&quot;)
  45. print(&quot;The resulting date will be:&quot;, new_date)
  46. break
  47. except ValueError:
  48. print(&quot;Invalid input. Please enter a valid integer.&quot;)

huangapple
  • 本文由 发表于 2023年6月8日 01:47:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定