1

I have a file that uses the random modules how do I include that module in my setup.py file?

Here is my code:

import sys
from cx_Freeze import setup, Executable

build_exe_options = {'packages': ['sys'], 'excludes': ['tkinter'],
                      'includes': ['random']}

base = None
if sys.platform == 'win32':
base = 'Win32GUI'

setup(name = 'name',
      verison = '0.1',
      description = 'description',
      options = {'build_exe': build_exe_options},
      executables = [Executable('fileName.py', base = base)])

My Script

import random
choices = ['rock', 'paper', 'scissors']
i = randome.randint(0, 2)
title = input('Rock, Paper, Scissor\nPress Enter to continue...')
instructions = input('Please read the instructions carefully...')
end = input('Please type \'done\' to end the game...')
enjoy = input('Enjoy!')
done = False
while not done:
    You = input('\nRock, paper, or scissors? \n')
    Computer = choices[i]
    if You == Computer:
        print('Tie')
    if You == 'rock' and Computer == 'paper':
        print('Computer wins!')
    if You == 'paper' and Computer == 'scissors':
        print('Computer wins!')
    if You == 'scissors' and Computer == 'rock':
        print('Computer wins!')
    if Computer == 'rock' and You == 'paper':
        print('You win')
    if Computer == 'paper' and You == 'scissors':
        print('You win')
    if Computer == 'scissors' and You = 'rock':
        print('You win')
    if You == 'done':
        exit()
Aidan L.
  • 79
  • 3
  • 14

2 Answers2

1

I have gone through both scripts you provided and there are a number of errors that I found they may be typing errors/ genuine mistakes I don't know but they would stop your script from working correctly.

  • if Computer == 'scissors' and You = 'rock': should be and You == 'rock': (two equals signs)

  • i = randome.randint(0, 2) should be i = random.randint(0, 2) (no e at the end of random)

Now for the setup script.

verison = '0.1', should be version = '0.1', (version not verison)

The error you are getting occurs because you are attempting to hide the console when no GUI is present.

base = None
if sys.platform == 'win32':
   base = 'Win32GUI'

base = None would mean that the console appears. base = 'Win32GUI' means that the console is hidden. You cannot do this because there is no GUI (such as Tkinter) to work with it.

To fix this problem just remove:

if sys.platform == 'win32':
   base = 'Win32GUI'

from your script.

If you do this you do not need import sys either.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • @Aidan L If you need any further help (or it doesn't work) please don't hesitate to ask me for help. – Xantium Nov 07 '17 at 19:29
  • @AidanL. To be honest I did not know this either until I tried an old script, then I realized it we both learnt something today. Happy to help : ) – Xantium Nov 07 '17 at 19:46
  • Simon, I have good news and bad news. First, the good news, IT WORKS, I TRIED OUT THE SCRIPT AND IT WORKS. Second, the bad news, when I open the exe file, it instantly closes. I opened it with the cmd, that works, but by itself, it didn't. I could use some help – Aidan L. Nov 08 '17 at 05:01
  • Right now, I am just adding an input at the end to stall the user for a random input to exit the application. – Aidan L. Nov 08 '17 at 05:14
  • @AidanL. There is nothing that can be done about that using cx_Freeze. If you try you script with the interpreter it will do that anyway. Adding `input()` at the end of each `if` statement is fine (that's what I always do -many others do as well) or use a GUI (something like Tkinter). If you are worried about the end user just put text with the input for example `input('Press any button to continue or enter "done" to exit.')`. – Xantium Nov 08 '17 at 10:34
  • @AidanL. No wait. You must have an error somewhere. Your script should not close until you type "done". Try launching your app from cmd and see if it generates errors. If not try recompiling and see if any errors or warnings appears there (just because it makes an app it does not mean that the app will work). – Xantium Nov 08 '17 at 10:45
  • Oh, I forgot to mention, I not using the script above, I just made a simple program that prints out a random between 0 to 1. (So I can test whether cx_Freeze works or not, I didn’t want to create or revise long lines of code in order for me to test it with cx_Freeze) – Aidan L. Nov 08 '17 at 16:04
  • But now I have another problem. I tried freezing my GUI program created with Tkinter. I included tkinter in the “packages” and “include” (I didn’t at “exclude since I don’t have anything to exclude) Everything was fine, but I build my setup, a couple of errors come. – Aidan L. Nov 08 '17 at 16:07
  • I did put a “Press any key to exit...”), and it works, so I just going to stick with it. – Aidan L. Nov 08 '17 at 16:08
  • @Aidan L. Sorry afk again. I'll get you some links (and help with Tkinter) later. – Xantium Nov 08 '17 at 18:27
  • @AidanL. First the input problem. There is another solution apart from the `input()` at the end of the line (not sure how useful it is) you can also use batch script. A script that would stop the console closing (Windows only) would be `@echo off`, `call `, `pause`. save it in a a file with something with `.bat` extension and double click bat file to launch. Oh if you do. However it works under Windows ONLY is that it has loads of advantages. Changing color of text/console, setting title just to name a few. – Xantium Nov 08 '17 at 20:13
  • @AidanL. Do you get the `KeyError: 'TCL_LIBRARY'` error? If not what are you getting? – Xantium Nov 08 '17 at 20:18
  • No, it’s something like can not find module tkinter or import error – Aidan L. Nov 08 '17 at 20:20
  • @AidanL. [You need `os.environ` to get all the Tkinter runtimes and libraries.](https://stackoverflow.com/questions/45401552/tcl-library-in-cx-freeze/45426617#45426617) – Xantium Nov 08 '17 at 20:25
  • So I import is, and then? – Aidan L. Nov 08 '17 at 20:27
  • @AidanL. Basically Tkinter has DLL Tk and Tcl plus a load of libraries for some (odd) reason it never seems to include those libraries so you must include them manually. – Xantium Nov 08 '17 at 20:30
  • @AidanL. I'll see what else I can find. – Xantium Nov 08 '17 at 20:32
  • So i import os, and then I link the tkinter or tkinter library, if the library , what is the path? – Aidan L. Nov 08 '17 at 20:32
  • @AidanL. Do you know where you installed Python? Generally `C:\Users\\AppData\Local\Programs\Python\Python\tcl/tk8.6` for Tk and `C:\Users\\AppData\Local\Programs\Python\Python\tcl/tcl8.6` for Tcl – Xantium Nov 08 '17 at 20:35
  • @AidanL. Find you Python location then it is always `\tcl/tk8.6` and `\tcl/tcl8.6` – Xantium Nov 08 '17 at 20:36
  • @AidanL. [This answer](https://stackoverflow.com/questions/46980781/problems-with-python-file-py-to-executable-exe-with-cx-freeze/47004528#47004528) might be more helpful. – Xantium Nov 08 '17 at 20:37
  • @AidanL. I did a really bad job where clarity is concerned -sorry. It is there somewhere – Xantium Nov 08 '17 at 20:38
  • Thank you, I check it later. – Aidan L. Nov 08 '17 at 22:54
  • @AidanL. OK then. – Xantium Nov 08 '17 at 23:06
  • I created a new file that just opens the `Tk()` window. Then I created a setup.py file and build it. All the extra files from the `tcllib` were imported. I ran the exe file, but it said `ModuleNotFoundError`. – Aidan L. Nov 09 '17 at 04:38
  • @AidanL. What does your setup script look like now (could you paste it below)? – Xantium Nov 09 '17 at 10:08
  • Oh, I just import tkinter `from tkinter import *`, then I assigned the variable `master` with the value: `Tk()`. I then add a `mainloop()` at the end of the code. That's it. – Aidan L. Nov 09 '17 at 17:39
  • @AidanL. I changed [this answer try it again](https://stackoverflow.com/questions/45401552/tcl-library-in-cx-freeze/45426617#45426617) I'll work on a working script in the meantime – Xantium Nov 09 '17 at 21:19
  • I am suppose to copy the `files` variable onto my `setup.py` file? – Aidan L. Nov 10 '17 at 06:37
  • @AidanL. Sorry I'm afk most of today so I can't get you a script working but I'll give you some background info so you may be able solve it yourself. The error is occurring because the tk and tcl DLL at are missing they need to be included along with your exe in the same folder (they are located in DLLs if you try and copy them manually. The folders tcl8.6 and tk8.6 need including as well (they are located in tcl). You can try and copy them manually see what happens but doing it the proper way would certainly be better. – Xantium Nov 10 '17 at 07:27
  • So I use os.environ and add the both DLLs? – Aidan L. Nov 10 '17 at 17:45
  • @AidanL. Yep. Or do it manually. -I'll get an example working within the next few minutes. – Xantium Nov 10 '17 at 17:59
0

This script will include everything but folders tcl8.6 and tk8.6. You will need to copy those with your build folder manually.

from cx_Freeze import setup, Executable
import os

files = {"include_files": ["<Path to Python>/Python36-32/DLLs/tcl86t.dll", "<Path to Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]}

os.environ['TCL_LIBRARY'] = "<Path to Python>/Python36-32/tcl/tk8.6"
os.environ['TK_LIBRARY'] = "<Path To Python>/Python36-32/tcl/tk8.6"

base = "Win32GUI"

setup(
    name = "Name of app",
    version = "0.1",
    author = "The author",
    options = {'build_exe': files},
    description = "Enter Description Here",
    executables = [Executable("tk_ex.py", base=base)])

EDIT:

This script works without copying and pasting anything.

from cx_Freeze import setup, Executable
import os
import sys
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR,'tcl','tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
files = {"include_files": ["<Location to Python>/Python36-32/DLLs/tcl86t.dll", "<Location to Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]}

setup(
name = "Name of app",
version = "0.1",
author = "The author",
options = {'build_exe': files},
description = "Enter Description Here",
executables = [Executable("tk_ex.py", base=base)])
Xantium
  • 11,201
  • 10
  • 62
  • 89
  • @Aidan L. Please take a look at the above. – Xantium Nov 10 '17 at 18:34
  • @AidanL. Right I found out how to do it. – Xantium Nov 10 '17 at 19:52
  • @AidanL. Ok. I don't know any other ways to do it so all I can suggest is a "hack". Build you exe as you did before (so you get the import error) go to the location where Python is installed. Go in `DLLs` folder and copy `tcl86t.dll` and `tk86t.dll` to the location where your exe is located. Next copy `tk8.6` and `tcl8.6` folders (located in `tcl`) to the location where your exe is and try again and see if it works. – Xantium Nov 11 '17 at 19:53
  • @AidanL. If this, the previous answers and the answer above does not work could you please copy the entire error message and I will see what I can do. – Xantium Nov 11 '17 at 19:55
  • Oh, I am just going to skip your first answer, and give you the errors later. I am on the phone right. The errors have nothing to do with tkinter module... I think. – Aidan L. Nov 11 '17 at 20:25
  • @AidanL. That makes sense. Ok let me know when it's convenient. – Xantium Nov 11 '17 at 20:49
  • Simon, as we can see, we’ve been getting a lot of errors with cx_Freeze. And no matter how we fix it, new errors pop up. I thinking of using something else to ‘freeze’ Python scripts into an .exe file. If so, Simon, do you have any recommendations for me on freezing Python 3 into an .exe file? – Aidan L. Nov 12 '17 at 07:32
  • @AidanL. Ouch. As far as I know Cx_Freeze is the only one that is still maintained. [Pyinstaller](http://www.pyinstaller.org/downloads.html) is the only other one I know you might have a chance of getting working. – Xantium Nov 12 '17 at 11:35
  • @AidanL. You might get more luck getting an answer if you post your question as another separate question. Someone else may be able to get it working. It seems unlikely that anyone else will read 31 comments to find your other question. Personally I would stick with Cx_Freeze because there is the danger Pyinstaller will become obsolete and you will be forced into Cx_Freeze anyway. Anyway you might find alternatives. If you do please tell me as I am not keen on Cx_Freeze either. Another thing: I have never used Pyinstaller but I have heard it is easier and generates single files exe. – Xantium Nov 12 '17 at 11:40
  • @AidanL. No problem. I hope you solve your problem soon. – Xantium Nov 12 '17 at 18:09
  • I downloaded PyInstaller, I used it to build from `.py` file into `.exe` file, but when I opened it said I am just missing one file, that is supposed to be in the build folder after the exe programs have been created, called `python36.dll`. – Aidan L. Nov 12 '17 at 18:57
  • @AidanL. Try copying python36.dll (located near my.exe) to the folder where the exe is located. This might work I have never used it pyinstaller but that sounds like what they want. – Xantium Nov 12 '17 at 20:21
  • Thanks, but I'm afraid that there are still more errors (the amount is about the same, when I used cx_Freeze) but I let's see if it might clear off some errors (since this error with the file seem to be the main error to everything) – Aidan L. Nov 12 '17 at 21:30
  • @AidanL. To help I need the trace-back. If it's Pyinstaller my help will be limited. – Xantium Nov 12 '17 at 21:38
  • Okay, I'll give it to you later. I am on my Linux (Raspberry Pi) and Windows – Aidan L. Nov 12 '17 at 22:00
  • Simon, I had finally succeeded building python to exe. I founded out when I build the `.py` script with PyInstaller, it creates two folders: `build` and `dist`. When I run the `.exe` file in the `build` folder. Everything fails, but that is different with the `.exe` file in the `dist` folder. Everything works!. Thanks for all your help Simon!! – Aidan L. Nov 12 '17 at 22:47
  • @AidanL. I might go for pyinstaller myself. Pleased I was able to help. : ) – Xantium Nov 12 '17 at 22:57
  • Yep, just make sure to open the ‘dist’ folder instead of the ‘build’ folder. – Aidan L. Nov 12 '17 at 22:58
  • @AidanL. Will do. Thanks : ) – Xantium Nov 12 '17 at 23:00