PYTHONPATH ignored

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
crobar
Posts: 160
Joined: Fri Aug 29, 2014 1:26 pm

PYTHONPATH ignored

Post by crobar »

Hi,

I would like to be able to use my own modules in FreeCAD scripts. I also have places where I prefer to keep these, and I add these locations to my PYTHONPATH environment variable.

Code: Select all

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
>>> print (user _paths)
  File "<stdin>", line 1
    print (user	_paths)
                     ^
SyntaxError: invalid syntax
>>> print (user_paths)
['', '/home/*****/src/python', '/home/*****/src/******/mechanical/freecad-python-modules']
>>> 
However, in the FreeCAD python interpreter the PYTHONPATH does not seem to be recognised:

Code: Select all

>>> import os
>>> user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
>>> print (user_paths)
['']
Is this expected, and is there a standard solution or workaround?
jreinhardt
Posts: 329
Joined: Mon Sep 09, 2013 6:08 pm

Re: PYTHONPATH ignored

Post by jreinhardt »

I guess the simplest solution is to use the -P commandline switch to add additional to FreeCADs path on startup. Or you could extend the path manually by using

Code: Select all

import sys
sys.path.append("...")
but this would need to be redone every restart.
BOLTS, standard parts library for FreeCAD Thread, Help with Translation
crobar
Posts: 160
Joined: Fri Aug 29, 2014 1:26 pm

Re: PYTHONPATH ignored

Post by crobar »

jreinhardt wrote:I guess the simplest solution is to use the -P commandline switch to add additional to FreeCADs path on startup. Or you could extend the path manually by using

Code: Select all

import sys
sys.path.append("...")
but this would need to be redone every restart.
Thanks, At the moment I have set up a a macro to add various paths, but it would be nice if I didn't have to manually run it at startup every time. The -P options might be the right way to do it, but I'm struggling to get this to actually work, I've tried:

Code: Select all

freecad -P /home/****/src/*****/mechanical/freecad-python-modules
but that doesn't seem to work (nothing is added to the path), is this the right syntax? I'm actually hoping I could just do

Code: Select all

freecad -P $PYTHONPATH
jreinhardt
Posts: 329
Joined: Mon Sep 09, 2013 6:08 pm

Re: PYTHONPATH ignored

Post by jreinhardt »

are you sure nothing is added? The arguments of -P are not necessarily at the end of sys.path. You can also try -M, I am not sure, what the precise differences are, both seem to add to sys.path.
BOLTS, standard parts library for FreeCAD Thread, Help with Translation
crobar
Posts: 160
Joined: Fri Aug 29, 2014 1:26 pm

Re: PYTHONPATH ignored

Post by crobar »

jreinhardt wrote:are you sure nothing is added?
No I take it back, the first syntax has in fact worked, I was obviously confusing myself somehow. The second (using $PYTHONPATH) does not, I assume (and will go test myself) that the syntax for adding multiple paths is just multiple paths separated by spaces?

It would be nice if it could handle paths separated by colons like in the PYTHONPATH variable, alternatively, if you could set a startup macro which is run every time FreeCAD starts, that would also be nice.

Thanks for the help
jreinhardt
Posts: 329
Joined: Mon Sep 09, 2013 6:08 pm

Re: PYTHONPATH ignored

Post by jreinhardt »

I haven't read the source, but I would guess that to add multiple paths, one just repeats the -P. One could probably expand the PYTHONPATH into this form with a bit of sed magic.
BOLTS, standard parts library for FreeCAD Thread, Help with Translation
peterl94
Veteran
Posts: 1001
Joined: Thu May 23, 2013 7:31 pm
Location: United States

Re: PYTHONPATH ignored

Post by peterl94 »

Yes, applications that embed python ignore PYTHONPATH to prevent the possibility of mixing up python versions. As jreinhardt said, FreeCAD provides the -P and -M options for modifying the path. The -M option is only for specifying other locations of FreeCAD modules (i.e. workbenches).
jreinhardt wrote:I haven't read the source, but I would guess that to add multiple paths, one just repeats the -P
I just tried this, and yes, that is the way to do it.
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: PYTHONPATH ignored

Post by keithsloan52 »

I am trying to test a source build of a python module/library that exists.

If I start with freecad -P <location of built>

Will FreeCAD not still pick up the original version?
Post Reply