SFML community forums

Bindings - other languages => Python => Topic started by: Benoit87 on June 30, 2013, 06:42:49 pm

Title: [Solved] cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Benoit87 on June 30, 2013, 06:42:49 pm
Hi everybody.
I used to work with pysfml-cython binding from Bastien Leonard, and it worked fine with cx_freeze compilation.
Today, I tried with the last PySFML official Jonathan De Wachter binding. It's a little bit different than Bastien Leonard's one and up to date.
I made a test with it and pymunk as physics engine.
And then, I juste tried to compile it with cx_freeze 4.3.1.
This is the error I got when I try to launch the executable.
(http://i.imgur.com/tdvs82y.png)

And this is my setup.py :
http://pastebin.com/51L5PkX3

Thanks in advance ! :)

PS : Sorry if I do some errors in English, I'm French.
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Sonkun on July 05, 2013, 10:52:47 pm
I'll check that as soon as I'm on Windows :)
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Benoit87 on July 05, 2013, 11:51:20 pm
Thanks, maybe we can find a solution  :)
But, perhaps only a modification on sfml binding or cx_freeze could correct that  ???
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Sonkun on July 05, 2013, 11:56:09 pm
Don't worry, we'll fix that :) If you had cx_freeze working with Bastien's bindings, then there's no reason we can't make it work since both bindings use Cython.
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Benoit87 on July 06, 2013, 12:11:16 am
I hope so ! :)
Hmm, then I think the problem could be about the use of sub-modules.
Indeed, bastien's binding doesn't use sub-modules (sfml.graphics, sfml.system, ...). Maybe cx_freeze can't handle that ?
And I noticed :
>>> import sfml
>>> sfml.graphics.Rectangle
<class 'sfml.graphics.Rectangle'>
>>> sfml.Rectangle
<class 'sfml.graphics.Rectangle'>
 
Maybe cx_freeze doesn't hunderstand something about that ?
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Benoit87 on July 23, 2013, 11:07:02 am
Hi again,
some news about the issue ?

Edit : If you prefer, we can go to french version ?
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: gaulois94 on July 23, 2013, 10:38:59 pm
I tried cx_freeze today, and I have the same problems. Creating executable for windows is very a difficult stage :D .
cx_freeze create 5 file : sfml.<module>.pyd (where <module> are graphics, window, network, etc.).
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Benoit87 on July 25, 2013, 11:20:18 pm
In general, for windows it's quite easy, but in this case, we got an embarassing problem  ???
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: gaulois94 on August 16, 2013, 06:26:43 pm
I up this thread for knowing if a solution could be find.
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Benoit87 on August 26, 2013, 12:20:37 pm
I up too, just in case.
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: gaulois94 on September 27, 2013, 08:09:28 pm
I up this thread again in the hope that we can find a solution about our problem :) (1 mouth it's a bit long, no ? :) )
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: rojohound on September 30, 2013, 11:04:46 pm
It's not an issue with pysfml, it's from using pymunk.  Specifically chipmunk.dll is not being copied over.  Copy that file over to the built folder and it works fine.

It actually will tell you it's the issue if you specify "Console" instead of "Win32GUI".
executables = [Executable("test.py", base="Console")]

After that you get a message like this in the console if chipmunk.dll can't be loaded.
Quote
Loading chipmunk for Windows (32bit) [C:\py\build\exe.win32-2.7\chipmunk.dll]

Failed to load pymunk library.

This error usually means that you don't have a compiled version of chipmunk in
the correct spot where pymunk can find it. pymunk does not include precompiled
chipmunk library files for all platforms.
...
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Benoit87 on October 01, 2013, 06:19:41 pm
Quote
I up this thread again in the hope that we can find a solution about our problem :) (1 mouth it's a bit long, no ? :) )
Yes, personnaly I still use bastien leonard binding instead until a solution :P

It's not an issue with pysfml, it's from using pymunk.  Specifically chipmunk.dll is not being copied over.  Copy that file over to the built folder and it works fine.

It actually will tell you it's the issue if you specify "Console" instead of "Win32GUI".
executables = [Executable("test.py", base="Console")]

After that you get a message like this in the console if chipmunk.dll can't be loaded.
Quote
Loading chipmunk for Windows (32bit) [C:\py\build\exe.win32-2.7\chipmunk.dll]

Failed to load pymunk library.

This error usually means that you don't have a compiled version of chipmunk in
the correct spot where pymunk can find it. pymunk does not include precompiled
chipmunk library files for all platforms.
...

I tried it, but my problem is the same.
I have this error now with PySFML 1.3 and chipmunk.dll in the repertory :
(http://i.imgur.com/gSDZvz3.png)
This time, it's import error : no module named "numbers"

But I do this in my setup : includes = ["numbers"]
And then, I have this again :
(http://i.imgur.com/Zu0VPtt.png)


Edit : I can give more details, the repertory where the executable is :
(http://i.imgur.com/3wc5ybh.png?1)

My setup code :
from cx_Freeze import setup, Executable
import sys

# Les dependances sont automatiquement definies, mais certaines sont
# parfois omises, on peut les ajouter manuellement ici :
includes = ["numbers"]
excludes = []
packages = []
path = sys.path

# On defini qu'il s'agit d'un programme graphique si on est sous windows. (pour plus tard)
base = "Console"
if sys.platform == "win32": # These lines were commented to test the Console base.
    base = "Win32GUI"

#On appelle la fonction setup pour creer notre executable.
setup(name = "PySFML & Pymunk Test",
        version = "1.0.0",
        description = "Un test de PySFML et Pymunk.",
    options = {"build_exe": {"includes": includes,
                             "excludes": excludes,
                             "packages": packages,
                             "path": path,
                             "optimize": 2}
               },
        executables = [Executable("pysfml_pymunk_test.py", base=base)])
 
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Anvilfolk on October 08, 2013, 12:08:40 am
Just registered because of this. I'm currently developing a Entity/Systems based engine along with a game called Planet 5521 (http://www.bay12forums.com/smf/index.php?topic=131635.0), which is open-source (https://github.com/Anvilfolk/nEngine), and some people have started asking for easier ways to try this rather than installing both Python3 and PySFML.

I'm trying to use cx_Freeze and having exactly the same problems.

My setup.py is very similar. The only difference is that I try to be more explicit about the packages:

build_exe_options = {"packages": ["numbers", "sfml", "sfml.system", "sfml.window", "sfml.graphics", "xml.etree.ElementTree"]}


So, essentially, I'd love to see this fixed as well. It'd provide a much easier way to deploy pySFML based applications. Seems like a fairly complex issue though. Here's to hoping!
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: rojohound on October 10, 2013, 06:14:04 am
I'd say it's an issue with using cxfreeze and python 3. Python27 works fine from my tests. I also tested python33 and it has the issue, I didn't test other 3x versions.  I've found some slightly similar issues with a google search of cxfreeze and python3, but nothing with solutions.

For reference for anyone digging further all that's needed to reproduce the error is a script that simply imports sfml.

I'd say the next step in debugging this is understanding what cxfreeze does and perhaps making a exe manually with the python c API to compare. For now using python27 could be a possible working option.
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: rojohound on October 10, 2013, 09:31:37 pm
Ok, I've come up with a solution for python 3.3.  The trick is copying sfml over manually and not letting cxfreeze do it.

My test.py
import sfml
 
My setup.py
import sys
from cx_Freeze import setup, Executable

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

buildOptions = {"includes":["numbers","re"],
                "excludes":["sfml"]
                }

setup(
        name = "Test",
        version = "1.0",
        description = "pysfml test",
        options = dict(build_exe = buildOptions),
        executables = [Executable("test.py", base=base)])

I then build with cxfreeze and then copy the sfml forder from "C:\Python33\Lib\site-packages" to the build directory.
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Anvilfolk on October 10, 2013, 09:32:48 pm
That's GREAT news!

Will try this as soon as I get home and report back. Amazing work!

Thanks!
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Anvilfolk on October 11, 2013, 01:52:29 am
The SFML errors disappeared, but unfortunately I'm getting an error that is easily reproduced. I am very unfamiliar with cx_Freeze, so I'm probably doing something wrong. Here's the example:

test.py
import mymodule.test

mymodule/test.py
print("Test successful")

mymodule/__init__.py exists and is empty.

$ python test.py
Test successful

$ test.exe
YADA YADA
ImportError: No modules named mymodule.test


I'm including the module explicitly, and I've tried both ways:
buildOptions = {"includes": ["mymodule.test"]}
buildOptions = {"includes": ["mymodule/test"]}

Any ideas?
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Anvilfolk on October 11, 2013, 02:06:30 am
If I use "include_files" to manually include the entire code, it appears to work. However, that replicates the entire directory structure along with the original source code in the build directory.

However, what I find strange is that library.zip includes all of the relevant .pyc files, so the code is there in compiled form. I shouldn't have to copy it over in source format.
Title: Re: cx_freeze pySFML compilation AttributeError, no attribute Rectangle
Post by: Benoit87 on November 05, 2013, 10:10:54 pm
Ok, I've come up with a solution for python 3.3.  The trick is copying sfml over manually and not letting cxfreeze do it.

My test.py
import sfml
 
My setup.py
import sys
from cx_Freeze import setup, Executable

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

buildOptions = {"includes":["numbers","re"],
                "excludes":["sfml"]
                }

setup(
        name = "Test",
        version = "1.0",
        description = "pysfml test",
        options = dict(build_exe = buildOptions),
        executables = [Executable("test.py", base=base)])

I then build with cxfreeze and then copy the sfml forder from "C:\Python33\Lib\site-packages" to the build directory.

Oh, great, your solution work fine with me also.

@Anvilfolk : Your problem is weird, and probably it's an issue from cx_freeze for python3.3 ? Because my project under python3.2 still work properly after been packaged by cx_freeze without particular setting like "include_files", and I import a lot of custom module (like "engine.main", "engine.physics.body", ...).
Moreover, in my test with pymunk (chipmunk) I include files called "functions.py", "main.py" and "classes.py". I haven't the problem with the setup.py here :
from cx_Freeze import setup, Executable
import sys

# Les dependances sont automatiquement definies, mais certaines sont
# parfois omises, on peut les ajouter manuellement ici :
includes = ["numbers", "re"]
excludes = ["sfml"]
packages = []
path = sys.path

# On defini qu'il s'agit d'un programme graphique si on est sous windows. (pour plus tard)
base = "Console"
#if sys.platform == "win32":
#    base = "Win32GUI"

#On appelle la fonction setup pour creer notre executable.
setup(name = "PySFML & Pymunk Test",
        version = "1.0.0",
        description = "Un test de PySFML et Pymunk.",
    options = {"build_exe": {"includes": includes,
                             "excludes": excludes,
                             "packages": packages,
                             "path": path,
                             "optimize": 2}
               },
        executables = [Executable("pysfml_pymunk_test.py", base=base)])

print("=============================")
print("Il faut copier manuellement le repertoire C:\Python33\Lib\site-packages\sfml\ dans le dossier exe.win32-3.3.")