[wingide-users] Changing Python Path from Script

Michael Foord fuzzyman at voidspace.org.uk
Fri Jan 12 17:34:51 MST 2007

Wingware Support wrote:
> On Thu, 11 Jan 2007, Michael Foord wrote:
>> We need to setup the project Python Path dynamically.
>> We sometimes work from several different branches, and we'd like to be 
>> able to just set the Python Path from a script based on the current file 
>> path.
>> Sorry to be a nuisance. How do we set the project Python Path from the 
>> scripting API ?
> Do you mean for running the script or in general?  Wing can store 
> a different path for each file, as accessed from the File 
> Properties in Wing.  That is used for debugging as well as source 
> analysis if the file is specified as the main file in the 
> project or (I think) when working in that file.
> If you want to set the path only during running, can't the script 
> the you wrote to execute code do that?
> In general, needing to set python path for each file like this
> raises red flags for me.  This is likely to cause you pain in the
> future whenever using or deploying the code. Maybe there's a
> better solution, for example redesigning things so this isn't
> necessary?
> If you must to set the File Properties automatically, you'ld have
> to call a function similar to gApplication.GetAttribute() where
> you call SetValue on the attribute manager instead (there isn't a
> way to set attributes via the API yet and we'll probably add
> specific API rather than access to attributes in general, tho I'm
> not sure yet).  The attribute in question is proj.attribs.kPyPath
> and it is used with a key for the file "location" (Wing's
> internal way of storing paths or files or urls).
> So usage would be something like this untested code:
>   import proj.attribs
>   import wingapi
>   from wingutils import location
>   filename = "/path/to/my/file"
>   mypythonpath = "fdjkd:fdsfds:fdsfds:yadayada"
>   loc = location.CreateFromName(filename)
>   fa = wingapi.gApplication.fSingletons.fFileAttribMgr
>   fa.SetValue(proj.attribs.kPyPath, loc, mypythonpath)
Thanks - this looks good.

> Hope this helps.  I'd still consider redesigning code so it runs 
> without a unique python path for each script.
Let me explain our use case. It shouldn't be that unusual.

All our imports are absolute imports based on the directory of our main 
script. This means that the Python path needs to be :

    Directory of main script
    Main Directory/Python24

We need this setting for autocomplete to work.

We often work on several branches, or rename our checkout directory. 
This means that for each branch we need to be able to set the Python path.

Having a simple script so that we can set this at the press of a button 
would be very convenient. This is not per file, but per checkout. My 
script can work out what the main directory of the file being edited is, 
and set the path appropriately.

The alternative is to have to dynamically create project files and 
reload this every time we switch to working on a different checkout 

All the best,

Michael Foord

> Stephan Deibel
> --
> Wingware
> Wing IDE for Python
> Advancing Software Development
> www.wingware.com

More information about the wingide-users mailing list