1

I am trying to create an executable for a script I have already written. It will be used by a coworker who doesn't have python on their machine, so I want to create an executable to make their life easier. I'm currently just trying to get py2exe to work and create an executable for a simple script that simply prints hello world and the constant e from the math module (just to get an idea for how py2exe works).

My setup script is as follows:

from distutils.core import setup
import py2exe
import math

setup(
console=[{'script':'hello.py'}],
options={
    'py2exe':{
        'packages' : ['math'],
        'bundle_files':1
    },
},
version='1.0.0'

)

where hello.py is simply:

import math
print('hello world!')
print(f'Here is e: {math.exp(1)}')

The error comes from this stack after I call "python setup.py py2exe" in cmd

Traceback (most recent call last):
  File ".\setup.py", line 5, in <module>
    setup(
  File ".\Anaconda3\lib\site-packages\setuptools\_distutils\core.py", line 148, in setup
    return run_commands(dist)
  File ".\Anaconda3\lib\site-packages\setuptools\_distutils\core.py", line 163, in run_commands
    dist.run_commands()
  File ".\Anaconda3\lib\site-packages\setuptools\_distutils\dist.py", line 967, in run_commands
    self.run_command(cmd)
  File ".\Anaconda3\lib\site-packages\setuptools\dist.py", line 1214, in run_command
    super().run_command(command)
  File ".\Anaconda3\lib\site-packages\setuptools\_distutils\dist.py", line 986, in run_command
    cmd_obj.run()
  File ".\Anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 192, in run
    self._run()
  File ".\Anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 273, in _run
    builder.build()
  File ".\Anaconda3\lib\site-packages\py2exe\runtime.py", line 250, in build
    self.build_archive(exe_path)
  File ".\Anaconda3\lib\site-packages\py2exe\runtime.py", line 490, in build_archive
    base = dist_path.rsplit('\\', 1)[0]
AttributeError: 'NoneType' object has no attribute 'rsplit'

2 Answers 2

0

To covert a .py file to .exe you need to install auto-py-to-exe

Windows

pip install auto-py-to-exe

After it is installed, open cmd then type

auto-py-to-exe

Then specify the path of the script and the location where you want to save the .exe file.

Sign up to request clarification or add additional context in comments.

Comments

0

Quick and dirty fix - I modified runtime.py (listed in the source of your exception) and added a check to continue if dist_path is None. It worked to compile a console application, but this might have unintended consequences for others.

if mod.__path__ is not None and mod.__name__[0] != '_': # attempt to select valid packages
    try:
        dist = pkg_resources.get_distribution(mod.__name__)
        dist_path = dist._provider.egg_info
    
        # Added if statement
        if dist_path == None:
            continue
                    
        base = dist_path.rsplit('\\', 1)[0]
        name = dist_path.split(base + '\\')[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.