Python paths madness.

dsp2017-1.png

I’ve spent whole evening to launch my models loader Python scripts. Since I didn’t remember anything, I was gladly surprised, that I left usage instructions at the beginning of the file.

""" 
Load and run this script in the Blender Console
e.g.:

import sys
sys.path.append("/path_to_cubicLoader_folder")
import cubicLoader
cubicLoader.run("cubicModel", frameStart, frameEnd)

requirements:
https://github.com/simplejson/simplejson
"""

I opened the source code of pythonLoader.py in Emacs. Copied the path through it:

~/git/cubicforest-java/python/jsonLoader/

And according to the instructions wrote in Blender’s Python console:

import sys
sys.path.append("~/git/cubicforest-java/python/jsonLoader/")
import cubicLoader

Result:

ImportError: No module named 'cubicLoader'

Ok, here it goes. Always something. So I’ve thought that maybe I previously had a different version of Python? Checked. No: the Python version is not guilty.

I started digging into the Python’s documentation about writing modules. I started with a simple ‘fibo’ module example. And it worked. I’ve created directory ‘~/fibo’, changed to it, created fibo.py. In console I’ve written like in my previous instructions:

import sys
sys.path.append.path("~/fibo")
import fibo
fibo.fib(10)

And no errors. The ‘fibo’ module is visible. So I’ve returned to Blender, tried again. Same errors. I’ve opened second Python shell and tried writing outside the Blender shell. Maybe it has some different default paths set. That would explain why fibo example works in normal shell and Blender shell gives errors. So again: I’ve tried exactly the same like like in Blender. Same errors.

Started to check many variations of sys.path.append having a fibo example as a guideline. Still nothing. Why fibo works and my loader not? I’ve changed pythonLoader.py file’s content to the same like in fibo.py. Loaded it in the same way. Still import errors.

I started to fiddle with a fibo example again… and noticed that commenting out sys.path.append.path(“~/fibo”) still results in a valid import. Ok, so more confusion. And then ‘aha’: the fibo.py is in the same directory as the launched Python shell. Import works without sys.append, because I’m in the same folder as fibo.py.

Great. So problem almost solved. I just have to do the same with Blender. Don’t minding why the import worked a year ago. Maybe I had a different Xubuntu installations with some paths set… So I’ve tried to solve the problem by making symbolic links in home directory, so the import will behave like in a fibo example: to make import from the current path.

Something moved forward: but other modules used by cubicLoader started to give import errors. Right: sys.path.append worked then, so now I’ve to make more symbolic links so the other modules could be found.

And then I started another try to make things work like they should. Opened the normal console, launched Python shell, split the console window and moved to the ~/git/cubicforest-java/python/jsonLoader path. Typed ‘pwd’ to get a path. Got /home/adamsko/git/cubicforest-java/python/jsonLoader. In Python shell:

import sys
sys.path.append("/home/adamsko/git/cubicforest-java/python/jsonLoader")
import cubicLoader

Silence. No errors. Confusion. Made a longer break, played http://phoboslab.org/xibalba/ (careful, addictive stuff!) for half an hour. Came back. Spot the mistake.

“~/git/cubicforest-java/python/jsonLoader” “/home/adamsko/git/cubicforest-java/python/jsonLoader”

Wind howling through my head.

Goodnight.

written in Emacs with org2blog mode

3 Replies to “Python paths madness.”

    1. When I used Emacs to copy the current path, it started with ‘~/’ (alias for a home directory in Linux). ‘pwd’ gives of course the full path starting with ‘/home/adamsko/’. Python does not know the ‘~’ :)

Leave a Reply

Your email address will not be published. Required fields are marked *