mikezang said:
how can I debug civ4 python code without run civ4?
By and large you can't. Civ4 needs to run because all the stuff from the CvPythonExtensions API can only loaded with Civ4, you can't run it seperately. That said, there are a few options available to you that can make your work easier.
1) Hotswapping. Civ4 allows you to hotswap Python code. This means that you can run Civ4 and your an editor with your Python code simultaneously and Alt-Tab between them. Whenever you change your code in the editor and save it, Civ4 will immediately reload the file, so you can instantly test the changes without having to restart the game.
2) Console. Civ4 has a Python console, this allows you to test and change the values of variables and the like while the game is running, a great debugging tool.
3) Syntax checking. If you have Python installed seperately of Civ4, you can let it check the syntax of your code without having to run the game, this can avoid errors while loading the game. Following is a slightly modified excerpt from a Python manual I'm working on:
To do this, type at the command line (in Windows: Start > Run… > type command and click OK):
C:\Python24\python -c "import py_compile; py_compile.compile('<fileName>')"
Where <fileName> is the name of the file you want to check. When you do this, Python will compile your script to byte code. Now, byte code is not terribly useful when using Python for Civ4, but if the compiler runs into a syntax error in the process of creating this byte code, it will terminate immediately and report the error. This process only takes a fraction of a second, so it's much faster than starting up Civ4 to see if your code is correct.
Of course, now you still have to type in a rather long and complex command every time you want to check the syntax of a file. So, why not let Python do that for you? Create a file called syntaxCheck.py (or whatever you want to call it) and paste the following code in there:
Code:
import os
import py_compile
strDir = []
for strFilePath, strDirList, strFileList in os.walk('.'):
for strFileName in strFileList:
strDir.append(os.path.join(strFilePath, strFileName))
[py_compile.compile(strEntry) for strEntry in strDir if (os.path.isfile(strEntry) and strEntry[-3:] == '.py')]
Create another file called sc.bat and paste the following in it:
Code:
@echo off
C:\Python24\python syntaxCheck.py
pause
Save both files and put a copy of them in every Assets\Python folder that you want to be able to syntax-check quickly. Now when you double-click sc.bat, Python will try to compile all files with the extention .py in the folder in question and in all its subfolders. A shell window will open and list the first syntax error Python encountered. If no errors are encountered, it’ll just prompt to you press a key to continue and you’ll know your code is free of syntax errors. Keep in mind that Python will only return the
first error it encounters, then it terminates. So after fixing the problem you’ll have to run sc.bat again to make sure there are no other errors.
Note that this code will leave a .pyc file in your folders for every .py file it encounters and successfully compiles. These .pyc files are of no use to you, you can delete them if you want (though there’s no harm in keeping them either, apart from cluttering your folders).
If you have an advanced editor, you can probably use the external tools functionality of that editor to simplify this process by doing the compiling directly from within the editor. For example, in my own editor EditPlus, I can go to Tools > Configure User Tools > Add Tool > Program and then set C:\Python24\python as command, -c "import py_compile; py_compile.compile('$(FileName)')" as argument and $(FileDir) as Initial Directory. These details vary per editor though, so if you use something else check the help info of your editor for details. Note that you should use the command line code above as basis for the editor settings (as opposed to the syntaxCheck.py or sc.bat file).
Better yet, if you use a Python-specific editor it may even have the ability to check code built in already. For example if you use IDLE, which by default comes with most Python distributions, you can load the file into IDLE and from the menu Run select Check Module. The program will then basically compile the file and automatically delete the .pyc file when it's done. It will even automatically take you to the line that caused an error if it encounters one.
But remember, these methods detect syntax errors and syntax errors only, so run-time and semantic errors are not detected. Those can only be found by loading Civ4 and running the game.