I'm running Vista SP2 : Python 3.3.5
I have a fairly large .py file (~11k lines) which I'm working on. I recently installed pyscripter and have been using it without issue for a week or so. But yesterday I went into pyscripter and as soon as I added a single new character to the file I got an "Out Of Memory" error. Sure enough it had maxed out all 2GB of ram on my laptop. I tried opening the file in IDLE, and although I could edit the file it would freeze up whenever I tried to run it. However in PythonWin the file opens and runs just fine. I commented out the function that I had last changed wondering if my code was causing the issue, but pyscripter and IDLE are still acting the same.
Anyone experience this before? Any ideas?
I think its to do with the way the GUI opens and displays the file, as i believe IDLE loads the whole thing into RAM when you open it, so the only way to get around that without changing your file would be to add more RAM, a more sensible approach would be to break up your file into smaller chunks, then add import statements at the top of the main file.
This also means that your file can be made easier to work with (assuming you break it up into sensible chunks where the functions are related in each file)
if you are worried about what will happen if you try to compile/package it up for use on other computers, i do this all the time with cx_freeze with no problems.
James
Related
I've just started learning Python and have downloaded PyCharm and understand this is likely to be a very basic question but wanted to ask before I set off with a false sense of security.
I'm interested to know whether:
a) Simply opening but not running a .py file as a project in PyCharm IDE could be malicious.
b) Opening and running a .py file in the PyCharm IDE can be malicious
If not either, why not? Would the file need to be a .exe to do anything without PyCharm being involved?
Again, appreciate any help/insight you can provide.
So just opening a file in PyCharm will not harm any computer in any way.
But running code can do that, so I would be careful were you get the code which you want to execute. And it would be best if you understood the basic of the code you are running. Therefore you would be able to spot strange lines in your code.
A Python source file (like any other programming language) is just a plain text file, so until the instructions that are written in the file are evaluated (by the python interpreter in this case) the file itself cannot be in any way harmful.
Only once you run python myfile.py in a shell or by using an IDE like PyCharm the code is actually executed... at that point what happens depends on what is written in the file (which you can check before executing it).
Alright just for context I'm an extreme newbie to python (around 15 minutes of experience).
My question is most likely a dumb one, but I'm gonna ask it anyways as I can't find anything about it elsewhere; Why can't I run the (very advanced) code below as a .py file? Using version 3.6 if it matters.
print("hello")
Thanks
Opening the python file will not cause it to execute.
Depending on your operating system (Windows, Mac, Linux) try opening a
command window and navigating to the file location of you python file.
Once you are in the directory (folder) of the python file that contains
the print statement run the following:
python myfile.py
Does that work?
Download and install
https://www.jetbrains.com/pycharm/download/#section=mac[Pycharm][1]
It is an IDE (Integrated Developer Environment). Pycharm will have what you need to write, save and execute python code all in the same application. There are others of course but Pycharm is as good as any to start with.
Good luck!
EDIT: I found out that the error is that the resources couldn't be opened. Copying the directory into the folder where the .exe is didn't fix it. I tried removing the resources from the .spec file and the size of the .exe file is now 9 MB as opposed to 52 MB so I'm pretty sure the resources are included, but somehow they can't seem to be opened by the .exe. In case anything is wrong with it, This is my .spec file - I only modified the Tree thing to include the resources, the .exe filename and icon.
In other words, the question now is: Why can't the exe find my game resources, and what do I have to do to fix that? Also, I realised my resources folder has two subfolders - does that mean I have to go about writing the Tree differently?
I've made a game in Python (using Pygame, too) and want to make a single executable file so I can distribute it. PyInstaller seemed perfect for that, and eventually, after a while of searching, I found out how to get it working in this guide. After I tweaked my code to get step 2 working I created the .spec file, added the directory with the resources to it (as in step 5, and including the font file), copied it to the same folder the .spec file was in and finally I ran
build.py game.spec
and I found the .exe file exactly where it was supposed to be. However, when I open it, it just closes again after a few seconds, and I think it's just before the main menu shows up. I'm not really sure it's a font screw-up again though because I made sure to include it in the resources as well...
Unfortunately, searching for a solution on the web didn't help me at all, especially because half the links to the project are broken now.
If anyone knows what's gone wrong here (or needs more details) please let me know.
Details:
Python version: 2.7.7 32 bit
Pygame version: 1.9.3 32 bit
PyInstaller version: 1.5 32 bit
System: Windows 8.1 64 bit (the 32 bit programs have all worked fine so far though)
Try to open command prompt and drag exe into it and press enter, that way you will catch the error (if there is any) and then update your question, more info == more help.
I never used pyinstaller so I can't really help you on that.
Also I suggest you to try py2exe, it worked nicely with pygame (at least for me) and later you can make setup with NSIS or Inno.
Edit:
If the .exe can't permanently save the highscores (as you said in comment below) try running exe as admin, it might be just matter of write privileges , or maybe it is your antivirus who doesn't allow it to write (sandbox at Avast, unknown/rare file at Norton).
I didn't find out what the problem was but I did manage to get it working. I started by installing the newer version of PyInstaller (2.1) with pip and completing the same steps again, but this time in the Scripts directory and running pyi-makespec and pyi-build instead of makespec.py and build.py because that's how the new version works, I guess. At first it would still complain about the resources being missing unless I ran it from cmd, but I think that was actually because I forgot step 5 (Tree in the makespec file). For some reason now the .exe can't permanently save the highscores, but apart from that it's working perfectly.
I am having some trouble with opening a .py file. I have a program that calls this .py file (i.e. pathname/example.py file.txt), but instead of running the python program, it opens it in Notepad. How to I get it to run?
The program itself takes in a file, and creates an output that is more readable.
Edit: The operating system is Windows 7. And the file that is calling the python is a .bat file.
Edit 2: It looks like I had to reinstall python for some reason... but it looks like it is finally working. Why reinstalling never comes to mind in the first place... And then I had to change how the file extention was opened. Thanks guys
This happened because most probably you have set notepad as the default program to open a .py file. Go to default programs app in windows. Select choose app by extension. Here search for .py files. Change the option from notepad to python. This should solve your problem.
okay.
1) i tried turning it off and on again.
2) i uninstalled and reinstalled python
still no joy. and then!
in windows explorer there's an open with option that sets the default program that windows is pointed toward if you click on the filename or enter it on the command line. change that from notepad or whatever it is if it's not python. change it to python. then presto. no problem-o.
You need to run it from the command line.
http://docs.python.org/2/faq/windows.html#how-do-i-run-a-python-program-under-windows
Are you trying to run the program like this?
/dirdir/MyPythonScript.py
try the following instead
python /dirdir/MyPythonScript.py
I just downloaded the latest Python IDLE 3.2 for Windows 7 (64-bit).
I can open, edit, and save existing .py files, but if I open a new document and try to save it, IDLE hangs.
After a few attempts, I tried using a regular text editor to create a hello world program (plain ASCII). But I cannot even open that file: IDLE hangs when trying.
I've searched the web and surprisingly found no hits for this problem. Does anyone have a workaround?
It appears IDLE doesn't cope with paths that have spaces in them. I worked around the problem by choosing directory paths that don't have spaces.