path problem

(Manuel) #1

My script don’t found a file under linux…
The file is a simple txt: pippo.txt
I’ve put it into the blender binary directory
and in the same dir of .blend that I use…

In the script
I’ve try to load with:

under windows it work simply with

(theeth) #2

did you try using an absolute path?


(Manuel) #3

Mhh…no, but I dont’want
a special path…I want only put the script and his relative
file (class, morphing, etc…) all in one directory: when the user
unzip the directory and open the .blend file, the script must
found all file …my wish is the user don’t modify the script by hand before use it…

But, in your opinion, where is the error in my test?
I’ve try all the combination… :frowning:

(theeth) #4

sorry, I wouldn’t know for sure, since I rarely use Python under Linux.


(eeshlo) #5

You could still try to use the absolute path, but the user doesn’t need to do anything. You can get the Blender executable path like this:

import Blender, sys, os

filename = "pippo.txt"

# file in blender directory
blenderpath = os.path.split(sys.executable)[0]
filepath = os.path.join(blenderpath, filename)

filepath then contains the path to your file.
Note that sys.executable returns the full path to the running python interpreter, so you only get the blender directory when you run this from Blender.

If the file is in the directory the blendfile was loaded from you can replace the last part by this:

# file in blendfile directory
blendfilepath = os.path.split(Blender.Get('filename'))[0]
filepath = os.path.join(blendfilepath, filename)

(Manuel) #6

Thanks!! :slight_smile:
When I’ve a little time I go to try…
But why simple path don’t work?

I’ve used sys.path to see the path (Now I’m using 2.23… :frowning: ):

[’/usr/lib/python2.0/’, ‘/usr/local/lib/python2.0/’, ‘/home/strubi/develop/lib/linux-glibc2.1.3-i386/python/lib/python2.0/plat-linux2’, ‘/home/strubi/develop/lib/linux-glibc2.1.3-i386/python/lib/python2.0/lib-tk’, ‘/home/strubi/develop/lib/linux-glibc2.1.3-i386/python/lib/lib-dynload’, ‘.’, ‘’]

the ‘.’ path is a relative path, right?

(jms) #7

In Blender2.23, try this:

Import Blender
print Blender.sys.dirname(Blender.Get('filename'))

(Manuel) #8

I’ve tested under Windows using b223, b225NAN, b225Niko,
the result is the same:

import os

no module named os


(jms) #9

Normal, there is no module os in blender
only in full python installation.

(Manuel) #10

Oh ho…it’s true excuse me for the stupid question…
the python path is a old problem for Blender.
I’ve python istalled…
To solve definitively the problem without esoteric procedure,
I’ve copied this file in the Blender dir:

In your opinion this is a wrong solution?
Can I distribute my script directly with this files?

(eeshlo) #11

You really should try to avoid that, you have PYHTONPATH set correctly? If so, have you tried any of the other often suggested methods, like setting the python directory in Blender (user defaults)?
Anyway, if you only need the os module for the filename stuff, you can use JMS’s suggestion instead for both Blendfile path and main Blender directory (the sys module is always available), or you could even code something yourself.