Install cx_Freeze in Anaconda virtual environment – Windows

cx_Freeze is a tool to create Python executable.

Steps:

  • Open Anaconda prompt. Goto Windows-icon. Type “Anaconda Prompt”. Select the suggestion shown.
  • Set the Anaconda virtual environment in the Anaconda prompt.
    • activate ch2_venv 
      • “ch2_venv” happens to be my virtual env. You choose yours.
  • then use pip to install like so:
    1. pip install cx_Freeze
    2. pip install idna
  • Create a new python file named setup.py in the current directory of your script and put the following code in it:
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = r'C:\\ProgramData\\Anaconda3\\envs\\ch2_venv\\tcl\\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\\ProgramData\\Anaconda3\\envs\\ch2_venv\\tcl\\tk8.6'

base = None    

executables = [Executable("SQLAlc.py", base=base)]

packages = ["idna","sqlalchemy", "urllib","pyodbc"]
options = {
    'build_exe': {    
        'packages':packages,
    },    
}

setup(
    name = "my_name",
    options = options,
    version = "0.1",
    description = 'My discription',
    executables = executables
)
  • Save the above setup.py file
    • Note: in the above code snippet for setup.py, I am setting TCL_LIBRARY and TK_LIBRARY paths directly. This is because I was having errors when I was trying to create an exe without it (cx_Freeze was unable to locate the tcl and tk folders). I also had to add pyodbc in the packages list as cx_Freeze was giving me error otherwise.
    • Note: in the above code snippet for setup.py, put the filename for file for which you want the exe here executables (I am having SQLAlc.py)
    • Note: in the above code snippet for setup.py, packages lists all the imported packages in your python code
  • now, in the terminal, type:python setup.py build
  • If all is good, exe will be generated
  • Check the newly created folder build. It has another folder in it. Within that folder you can find your application. Run it.



No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *