how to compile a python .app from ubuntu or windows - python

I am still very new to Python and any freezing programs.
Recently I made a very short text adventure game I'd like to send to a few friends, most of whom have Snow Leopard. I don't want them to have to bother downloading python just to play the game though.
Is there a way to convert my game from Ubuntu so that it is playable on Mac? That is, make an .app file from ubuntu? Or even from Windows, I suppose.
I tried using cx_freeze on Windows but that just compiles an exe which is not playable on Mac.
Thanks for any help and suggestions.
EDIT: I am using Python 3.2.2. I think Macs come standard with an older version else there would be no problem just sending them the game, I imagine.

As far as I remember, last I used Mac OSX, it already had Python installed. This was with Mac OSX Snow Leopard.

No need do compile to a native binary if you can run your code through Python on any machine.
Your code could run in Python 2.6, the default which comes with Snow Leaopard, unless you used version 3 specific features. Try if it does. If so just package your code so it runs the .py through python /path/to/code.py
You can also make your .py file self-executable with a shebang first line,
#! /usr/bin/env python
# this should be the first line in your .py file
and setting the file executable flag, perhaps through your packager on installation:
$ chmod +x code.py
You will notice shebang does not point to a specific python binary like /usr/bin/python, but to the /env environment path, so it will use the default interpreter on whichever machine it is run on.

Related

How to force a Python .pyw executable to open in a specific Python version?

I have a program that I wrote a while back and converted into a .pyw executable file. It worked fine but recently I installed a different version of Python so I could use an outdated library, and now my .pyw won't open.
I'm assuming this is because it doesn't know which version of Python to use. The only way I can get it to open is to right-click the file, and choose to edit in IDLE 3.8 32bit, then run from there. Its very long winded compared to just running the file from my desktop. Of course I can also use the command line but its also longwinded.
Is it possible to add a line of code to the file which will force it to open in a specific Python version? I need to be able to specify the version and also x86/64, since I have Python 3.7, 3.8(64) and 3.8(32).
Note: please don't get onto me about having multiple versions installed I'm well aware that its not ideal but it was necessary for a project I'm doing :)
If you had installed Python with the py launcher on Windows, you can set a shebang line with Python 3.8.
For your case, you might want # python3.8-32 or # python3.8-64 or # python3.7.

Bundling Python script to run on different Macs

I wrote a Python script that scrapes PDFs that are in the same directory as the file itself.
I used pyinstaller on my Mac to convert this to a onefile .exe on my MacBook Pro and it works great. However, when I try to send this file to someone else via email, it doesn't open because "it's a Windows file", despite the fact that it was compiled/bundled on a Mac. The modules I use are regex, PyPDF2, among other non-standard libraries. How do I make these executables run on different computers?
I've tried using auto-py-to-exe, but to no avail.
The executables work as built on my Mac, but when I email it to someone whose Mac does not have Python/IDLE installed, I run into a wall. If it makes a difference, I've made a version for Windows, and that works great, too.
AFAIK, you need to compile Python programs separately for each environment. If you're on Linux, give crossenv a try.
If you aren't on Linux, to build packages that work on a Mac without IDLE installed, try py2app.
You can also suggest that whoever you're sending this file to install wine, which will let them run the .exe (though installing python might be a tad easier for them.)

Python shebang not working

Python is installed at:
C:/Python/Python35
At the top of my program I put:
#!/usr/bin/env python3
I opened windows command prompt and entered:
./words.py
The message I got was:
"." is not recognized
I was told this should work great on Windows so I'm confused?
./words.py will not work on the Windows command prompt, this way of executing scripts is meant for Linux/UNIX shells.
If you're using Python 3.3+:
The shebang lines will be obeyed by the Windows Python Launcher py if you have it installed (https://docs.python.org/3/using/windows.html#shebang-lines).
Make sure you have that installed and try launching using:
py words.py
There are multiple problems here.
Shebangs are a Unix thing. Windows does not recognize them at all. Instead, Windows uses file extensions.
There is no file named /usr/bin/env on Windows, so even if your shebang did work, it wouldn't do what you want.
The Python installer normally associates Python with .py files on installation. If it did not in your case (because you disabled the option?) you need to go fix that.
./something doesn't work on cmd.exe. It's .\something or just something.
Whoever told you, must have either misunderstood you or you misunderstood them.
Shebang lines have no effect with windows unless you're trying to use cygwin. The other reason you'll need them to ensure smooth transition between windows and linux if you're passing the code to someone who might be running on linux.
As far as the "." it is not used in windows. Its reason in linux OS is to inform the command that the script is in the current directory.
For windows all you'll need is: python words.py.
Your first problem is that Windows won't treat / as a path separator here. It has to be .\words.py or just words.py. That's why you get the error you see.
The next problem is that the shebang is not implemented by the windows command shell. It works on linux shells (and linux-like shells on Windows such as cygwin) because those shells read the front bit of executables to see how they should be executed. Windows does this by looking at the extension.

How to install/run Python on a Mac

After giving up on doing a disk partition and getting Linux, I decided to try and install Python 3.3.2 on a 32 bit Intel Core 2 Duo iMac with OS X 10.6.8 Snow Leopard. I did pretty much everything the official website says, but it isn't working at all.
First of all, I downloaded the installer for Mac OS X 6 and later, which should supposedly put a folder called MacPython 3.3 with IDLE, PythonLauncher and a Build Applet tool in my Applications folder. What I have instead is a folder called Python 3.3 which does contain (aside from the documentation and a command to update shell profile) IDLE and PythonLauncher, but no Build Applet tool; I typed 'python' into my Terminal window to see if 3.3.2 was installed, but it said Python 2.6.1 even after restarting my computer. The ReadMe in the disk image that contains the installer says running the installer should also 'put the underlying machinery into the folder /Library/Frameworks/Python.framework'. I did every search imaginable and apparently there is no such folder anywhere in my hard drive.
Basically, I want to install 3.3.2 and run things in programme mode rather than shell mode, which I find to be a huge pain in the bottom. Any help is greatly appreciated.
You don't need to download or install any software.
Just write your code in your preferred text editor, and save it as a anything with .py at the end. Next, go to a terminal and type in python *****.py, where ***** means whatever your file is named.
Try typing 'idle python3.3' or something similar in the terminal.

Python 3.2 doesn't work with python 2.7

I already have python 2.7 (installed using activepython). I'm trying to use python 3.2 just to learn more about it so i downloaded it from python.org
python 2.7 still works perfectly, but python 3.2 gives me this error when i try to open the ide.
and then I see the send error window, if i install python 3.2 using activepython i see the same error.
I'm using windows xp pro sp3 32 bit and i had the same error on sp2... How do I fix it?
EDIT #Zuljin
This is the first time that i use dependancy walker so could you give me a hand please
this is what i see
what does that mean? I already have these files...
I saw a lot of answers here. But, I think the file that you are trying to run is not correct
C:\Python32\Pythonw.exe
is not the one you use to open idle.
Open
C:\Python32\Lib\idlelib\idle.pyw
Python 2.x and 3.x can cohabitate perfectly in win xp and win 7, either 32 or 64 bits.
If you first installed the ActiveState Python distribution, be careful when installing python-3.2.2.msi.
When you arrive to the customize python 3.2.2 screen, (un)check 'register extensions' to make the feature unavailable (that is: do not register extensions).
I have installed this way both distributions in different computers without any problem so far.
Note: Check your environment variables. Only python 2.7 should be in the path (if you installed before the two distros you could have them both in the path. Remove python 3.2 path)
Edit:
From Ankit post I realized that in fact you were trying to open IDLE, maybe.
If you follow my installation instructions when you call idle.bat from the py3.2 idlelib folder you actually get idle for python 2.7 as this is what the call find in the windows path. To be able to open idle for py3.2 in the presence of python 2.7 as the registered python, I use a .bat file modified from that in the distribution:
idle_stay.bat
#echo off
rem Working IDLE bat for Windows - Custom bat for py3k as secundary python
C:\Python32\pythonw C:\Python32\Lib\idlelib\idle.pyw %1 %2 %3 %4 %5 %6 %7 %8 %9
The name idle_stay.bat is because I put this file in the folder where the official idle.bat is (C:\Python32\Lib\idlelib\idle_stay.bat). In this way it does not get overwritten each time I unisntall and reinstall a new version of python 3
It seems to me you have associated a *.py or *.pyw file extension type to some version of python.exe and/or pythonw.exe you aren't clear about. This is often, but not exclusively done from within Microsoft's "explore.exe" File Manager (I believe in Options).
I disagree that this is a reliable method:
C:\Python32\Lib\idlelib\idle.pyw
as this method will run whichever program is assigned to the "pyw" extension. That could be:
C:\Python32\pythonw.exe
or
C:\Python26\python.exe
or whatever might have happened to your file extension association as per OS or bad PATH environment variable setting. It is better to do something like this:
C:\Python32\pythonw.exe C:\Python32\Lib\idlelib\idle.pyw
where you are explicidly telling the OS to run the python executable from a specific path and selecting a specific python script in the file/folder hiearchy.
Another thing you can try to gather info is just:
python.exe -V
which will tell you the version number.
Another thing you can do:
Open a DOS command prompt and type:
set PATH=C:\Windows;C:\Windows\system32;C:\Python32
python.exe and note the version and whether is runs.
exit()
exit
Once working debug your PATH. You can use this Python script to gather intel.
If you turned off your WinXP findfile stuff (like I have) you can use this script:
inpath.py can help shed some light.
Run this python script like this:
Drop to a DOS prompt
cd to your folder with a working python.exe version.
and type python.exe inpath.py python*
where inpath is in that same folder for this test/debug.
inpath.py will search your path for all files associated as "runable" in your OS and that is in your "PATH" with the python*.* pattern.

Categories

Resources