Python path woes

Posted by Poromenos on Sat 28 Jun 2008 12:16 PM — 11 posts, 36,933 views.

Greece #0
I'm writing a script file and I've broken some of the functionality into modules, but when I try to import them I get import errors. I assume this is because MC runs the interpreter from its working directory and thus the interpreter can't find my module.

Is there any way to tell it where to look? (I don't want to have to put my module in site-packages)...
Australia Forum Administrator #1
I have been having some similar problems recently. The "current directory" is a pain, as it can change if the user simply browses for a file in another directory.

A couple of solutions:


ChangeDir (GetInfo(66))  --> make the current directory the MUSHclient directory


Then load your modules.

Or use something like GetInfo(66) and then add the rest of the path to it.
Greece #2
Hmm, can MC be changed so that the interpreter runs each script from its directory?
Netherlands #3
No, it came up before in these forums I think. Every process only has one current directory, so you can't make it specific.

However, Python is a wonderful language. Not too long ago, I read somewhere about someone who adjusted the way importing works so it would first look in a specific directory. Google a bit, you shouldn't have too much trouble finding it. Otherwise I'll see if I can find it.
Greece #4
Ah, I've already done that, all you need to do is append the directory of the import files to sys.path, I just don't like hardcoding paths :)
Australia Forum Administrator #5
Can't you use GetInfo (66) ? That isn't a hard-coded path, it is the path currently in use.
Greece #6
Yes, I think I'll go with that, I was just wondering if there was a way to "fix" this. I'm not sure whether each loaded script has a path one could tweak... Anyway, I'll use GetInfo, thanks for your help :)
Greece #7
Hmm, that points to the world file directory, which assumes the script is in that directory... Could you at least add a "Script directory" directive there? Luckily, both my world file and its script are in the same directory, so this solves it.

I'm trying to do my thesis with MUSHclient and I run into a few problems on the first day :P By the way, it's GetInfo(67), small oversight there.

Thanks again for your help!
Australia Forum Administrator #8
66 is the application directory, like I said. I think that is always a fully-qualified path name, which is why I used it.

67 is the world file directory. That could be anywhere, it depends where you put your world file.
Netherlands #9
Also, since I am not sure if you want to refer to own utility libraries which aren't MUSH specific, or whether you are just trying to split your plugin over multiple files, I think the <include> tag may also be a viable alternative. I use it for one of my plugins which have around 2500 triggers in it, and where having it all in one file would be... TOO bulky.

It's essentially a concatenation of all scripts over the files into one buffer, which is loaded as one file. As such, it is all the same namespace and can't be seen seperately from within Python. But for my uses, that's usually more than plenty. =)
Greece #10
Ah, I can't use that because it's a plain script file, not a plugin file. The path thing should work well, thanks!