• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Liuqahs15

C++ & Python - Code Executes and Closes Immediately

6 posts in this topic

I've embedded Python into C++ successfully (without even remotely as much pain and agony as with Lua). Now when I run a simple test program, the console window opens and closes immediately. It definitely executes the way it should with no problems, however I seem to be unable to stop it from closing immediately. I've called system("PAUSE") and added cin >> variable so the program basically has absolutely no choice but to wait for input, yet these things are somehow ignored. I'm on Windows 7 using Visual Studio 2012. Here's the code:

[CODE]
#include <Python.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
int x = 0;
cin >> x;
system("PAUSE");

return 0;
}
[/CODE]

Any idea what's going on here? Oddly, when I place that code that's supposed to pause it

[CODE]

int x = 0;
cin >> x;
system("PAUSE");
[/CODE]

in BEFORE all the python code, it does actually stop. Even worse: I've used breakpoints. If placed on or before the call to Py_Initialize(), the code stops at the breakpoint. Anywhere else, the breakpoint is never reached. Strangely, VC++ will usually tell you if a breakpoint will never be reached, however it seems to believe that the breakpoints placed after Py_Initialize() will be reached.


UPDATE::

So apparently there's an error occuring. I barely noticed it. What happens is, a message is output "Import Error: No module named site." The program then closes immediately. Any ideas? I'm googling now, and I'll be back with another update if I find an answer, for future readers. Edited by Shaquil
0

Share this post


Link to post
Share on other sites
Annoyingly, the python DLL isn't self contained, it depends on several python files to run properly. Which version of Python are you embedding? With the 2.6 series or earlier you can zip up the contents of your Python/Lib directory into a file that you can stick in the Python path. 2.7 calls initsite() before you can set the the python path meaning that you need to place the relevant .py files in just the right spot for Py_Initialize() to work properly.
2

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1352209340' post='4998020']
Annoyingly, the python DLL isn't self contained, it depends on several python files to run properly. Which version of Python are you embedding? With the 2.6 series or earlier you can zip up the contents of your Python/Lib directory into a file that you can stick in the Python path. 2.7 calls initsite() before you can set the the python path meaning that you need to place the relevant .py files in just the right spot for Py_Initialize() to work properly.
[/quote]

Thanks a lot for such a quick response. I'm on 2.7.3. So is there a place where I can find out what .py files to put where? Probably not, right? I'll keep googling and get back with an answer if I find one.

By the way, for clarity:

The error I was getting was "Import Error: No moduled named site."

After I fixed the environment variable in VC++ to PYTHONHOME="E:\Python-2.7.3\" nothing changed. That happens to be the path to python on my hard drive, by the way. If you're reading this, yours might be in C:\Python27\ or something like that
0

Share this post


Link to post
Share on other sites
Yeah, in 2.7 initsite() is called [i]before[/i] PYTHONHOME is processed. You might find this [url=http://thejosephturner.com/blog/2012/02/18/the-perils-of-embedding-python-2-7/] blog post[/url] informative. I basically have given up on embedding 2.7 and just use 2.6. Edited by SiCrane
fixed url
2

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1352216001' post='4998062']
Yeah, in 2.7 initsite() is called [i]before[/i] PYTHONHOME is processed. You might find this [url="http://thejosephturner.com/blog/2012/02/18/the-perils-of-embedding-python-2-7/"] blog post[/url] informative. I basically have given up on embedding 2.7 and just use 2.6.
[/quote]

I followed your advice and downloaded 2.6.8. Now when I run the code, everything's fine but I get the message 'import site' failed: use -v for traceback. That message is printed in the console. Other than that, it works fine. I'll test it out some more to see if this has any serious rammifications, or if there are any solutions. It's been hard finding any on google.

Thanks again for all your help by the way, it made a huge difference.



For those looking for instructions:

I built the library by opening the PCBuild folder and simply opening the vcproj and building it in visual studio. The python26_d.lib and python26_d.dll files will appear in the PCBuild folder. You'll have to include those in the linker, and that's about it. You can PM me if you're confused.
0

Share this post


Link to post
Share on other sites
That error means that it still isn't finding the site.py file. Switching from 2.7 to 2.6 doesn't mean you don't have to supply the file; it just gives you more options on where you can put it and how to tell Python where it is. I generally zip up the .py files I need from the Python Lib directory and put that zip file in sys.path.
2

Share this post


Link to post
Share on other sites
That fixed it. In VC++ 2012 I went to Project->Properties->Debugging and under Environment I entered

[b]PYTHONPATH=E:\Python-2.6.8\PyModules.zip;[/b]

At first it didn't work because I forgot the semicolon. Thank you [i]so much[/i]. You were amazing help!

EDIT: I realized later how awful that explanation was. I copy/pasted the entire contents of E:\Python-2.6.8\Libs\ into a zip folder I made, PyModules.zip, and then set the environment variable that way. So that's how you do it. Edited by Shaquil
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0