0

I'm trying to do this little tutorial http://www.roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod,_part_1

A little ways down the page right before it says moving around it says to test what you have so far. I'm using Pycharm and this is my first time using an outside library or whatever you call it.

This is what I have so far and it is exactly what is in their example:

import libtcodpy as libtcod

#actual size of the window
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50

LIMIT_FPS = 20  #20 frames-per-second maximum


libtcod.console_set_custom_font('terminal.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)

libtcod.sys_set_fps(LIMIT_FPS)

while not libtcod.console_is_window_closed():

    libtcod.console_set_default_foreground(0, libtcod.white)

    libtcod.console_put_char(0, 1, 1, '@', libtcod.BKGND_NONE)

    libtcod.console_flush()

Whenever I run it I get this error.

Traceback (most recent call last):
  File "D:\Programming\Project 1\Rogue Like\libtcodpy.py", line 57, in <module>
    _lib = ctypes.cdll['./libtcod-mingw.dll']
  File "C:\Python34\lib\ctypes\__init__.py", line 426, in __getitem__
    return getattr(self, name)
  File "C:\Python34\lib\ctypes\__init__.py", line 421, in __getattr__
    dll = self._dlltype(name)
  File "C:\Python34\lib\ctypes\__init__.py", line 351, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Programming/Project 1/Rogue Like/firstrl.py", line 1, in <module>
    import libtcodpy as libtcod
  File "D:\Programming\Project 1\Rogue Like\libtcodpy.py", line 60, in <module>
    _lib = ctypes.cdll['./libtcod-VS.dll']
  File "C:\Python34\lib\ctypes\__init__.py", line 426, in __getitem__
    return getattr(self, name)
  File "C:\Python34\lib\ctypes\__init__.py", line 421, in __getattr__
    dll = self._dlltype(name)
  File "C:\Python34\lib\ctypes\__init__.py", line 351, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

Thanks

HunterWard
  • 58
  • 2
  • 8

2 Answers2

1

I'm assuming you also copied libtcod-VS.dll or libtcod-mingw.dll to the project directory, not just libtcodpy.py. And also SDL.dll and a arial10x10.png. If not, go back and look at the Setting it up instructions again.

But if you have, this isn't really your fault, it's theirs.

libtcodpy.py tries to import the libtcod-VS.dll or libtcod-mingw.dll DLL from the current working directory. You can see that from this line:

_lib = ctypes.cdll['./libtcod-mingw.dll']

So, if the current working directory happens to be anything other than the directory that libtcodpy.py is in, it won't find them there.

This is a silly thing to do. If you do what the Choice of code editor section suggests and always run the script from a console (a "DOS prompt"), it will work (as long as you're always running it without an explicit path), but they really shouldn't be relying on that.

Still, that's obviously the simplest fix: Run the program from the console, the way they're expecting you to, instead of from PyCharm.


Alternatively, you can configure PyCharm to run your project with the project directory as your working directory.

There are a few ways to set this, but the one you probably want is the Run/Debug Configurations dialog (which you can find under Edit Configurations… on the Run menu). Open that dialog, open the disclosure triangle to Defaults, click Python, then look for "Working directory:" on the right. Click the button and pick your project directory (or wherever you put libtcod-VS.dll or libtcod-mingw.dll).


Or you can edit libtcodpy.py to make it look for the DLL alongside of itself, rather than in the current working directory. There are only 4 small changes you should need.

First, in the middle of the import statements near the top, if there's no import os, add it.

Next, right after the import statements, add this:

modpath = os.path.dirname(os.path.abspath(__FILE__))

Now search for the two lines that start with _lib = ctypes.dll (or just look at the line numbers from the tracebacks) and change them as follows:

_lib = ctyles.cdll(os.path.join(modpath, 'libtcod-mingw.dll'))

_lib = ctyles.cdll(os.path.join(modpath, 'libtcod-VS.dll'))
abarnert
  • 354,177
  • 51
  • 601
  • 671
0

I've just been struggling with the same problem myself, though I'm using Emacs and Python 2.7.

What solved the problem for me was installing a 32-bit python instead of a 64-bit python. The .dlls in libtcod are 32-bit, and 64-bit python on Windows isn't compatible with 32-bit .dlls.

Also, you might want to check if libtcod is compatible with python 3. I've found two places where the subject is discussed, but I can't tell if libtcod-1.5.1 is compatible with the later 3.xs.

I'd also suggest trying to run the samples_py.py in the libtcod folder to test these two problems, as if that runs it's your folder setup or path, rather then your version of python.

eoinr
  • 1