Home Download Buy Blog Forum Support

Python build - dynamic env / multiple cmd

Python build - dynamic env / multiple cmd

Postby alec.tron on Thu May 30, 2013 3:14 am

Hi,
I'm trying to get sublime_text internal console/build to work in a somewhat complex need/env system where the context & allowed versions can change a lot. I need the build system to be easily switchable, ideally dynamically.

This
/Packages/Python/Python.sublime-build
seems to be the key.
This is default
{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
}


So there could be 2 ways.
One could be declaring pythonpaths. i.e.
"env": {"PYTHONPATH": "/bla1/lib"}
Unfortunately in my case, these consist of dozens to 100s of libs in various version, depending on context. So altering those depending on where it needs to be run is impractical.

So the next idea was to use multiple cmd issues that build upon each other. But looks like multiple cmd statements are not supported in the same line:
i.e.
"cmd": ["setMyEnv", "/giveMe/env1","&","python", "-u", "$file"],
fails with:
[Errno 2] No such file or directory

and if I do it in consecutive commands (setMyEnv in a linux shell would set the libraries/module version as I need them), which runs through a build fine, i.e.
"cmd": ["setMyEnv", "/giveMe/env1"],
"cmd": ["python", "-u", "$file"],

but the 2nd cmd does not know about the first, and I am still env-less.

This seems to be something that regularly pops up for others. See
viewtopic.php?f=3&t=6386&p=27708&hilit=build+cmd+multiple#p27708 )
http://sublimetext.userecho.com/topic/8 ... ld-system/
viewtopic.php?f=3&t=4370 (offers a shell=true solution, but that does not seem to work for me)

Has anyone got any tips how this could be tackled ? Or is that something that's being looked into in sublime dev ?

Cheers.
c.

ps
there's also this,
http://blog.pcitron.fr/2013/02/08/multi ... ld-system/
again with shell+true, for Windows.
So for Linux this should be:
"cmd": ["setDynamicPythonEnv", "env1", ";" "python", "-u", "$file"],
But no matter which syntax I try for ";", I end up with this when the linux syntax seems to be working:
[Errno 2] No such file or directory
And a sublime build error message when the syntax is bad:
"Error trying to parse build system: Unexpected character, expected a comma or closing bracket in ~/.config/sublime-text-2/Packages/Python/Python.sublime-build:4:45"
Last edited by alec.tron on Mon Jun 10, 2013 5:21 am, edited 1 time in total.
alec.tron
 
Posts: 5
Joined: Thu May 30, 2013 1:18 am

Re: Python build - dynamic env / multiple cmd

Postby alec.tron on Fri May 31, 2013 4:56 am

Hm...
looking around further for a solution for sublime_text 2, I had a look through the v3 beta builds, and this could be what I need....
"
Build 3006
Release Date: 29 January 2013
...
Build Systems: Added 'shell_cmd', which supersedes 'cmd', with more intuitive syntax
"
does that allow multiple cmd commands that built on top of each other ?

Cheers for your ears.
c.
alec.tron
 
Posts: 5
Joined: Thu May 30, 2013 1:18 am

Re: Python build - dynamic env / multiple cmd

Postby alec.tron on Sun Jun 09, 2013 11:12 pm

Hey,
I thought this might be a trivial question and people have solutions for this to use the in-built python shell, but after spending some time on it I'm close to giving up on a feature I deem not quite essential but great to streamline the workflow...
Apparently though, after talking to the sales team, support is only granted by measuring forum/thread activity. So if anyone else would like a way to set up (python) build environments more dynamically than what a single cmd or hard-coded pytonpath allows, please post here.
Or, if you found a way how to deal with this inside of sublime and regularly changing project/needs/versions/pythonpaths, please sing out.
Churs.
c.
alec.tron
 
Posts: 5
Joined: Thu May 30, 2013 1:18 am

Re: Python build - dynamic env / multiple cmd

Postby fjl on Tue Jun 11, 2013 6:51 am

As far as I understand, you want to make two things happen when building:
  • Set some environment variables
  • Run python
Why not write a script that does both things? E.g.
Code: Select all
#!/bin/sh

PYTHONPATH="..."
export PYTHONPATH

python $@

If you need many environments, just make N copies of the script and create a Sublime Text build system for each.
fjl
 
Posts: 36
Joined: Wed Dec 28, 2011 12:41 am

Re: Python build - dynamic env / multiple cmd

Postby schlamar on Tue Jun 11, 2013 7:56 am

There are already solutions for this issue which are not Sublime specific. Please have a look at virtualenv/virtualenvwrapper. So execute in a virtualenv with virtualenvwrapper in Linux would be as simple as "workon envname && python run $file".

Please note that the syntax in each of your examples is wrong. It should be just like http://blog.pcitron.fr/2013/02/08/multi ... ld-system/. On Linux executing a shell manually and separating commands with "&&", everything should be in one string. On Windows set shell=True and separate commands by "&;".
schlamar
 
Posts: 107
Joined: Tue Feb 19, 2013 2:11 pm

Re: Python build - dynamic env / multiple cmd

Postby alec.tron on Wed Jun 12, 2013 2:54 am

Hey guys,
thanks for the replies!
fjl wrote:If you need many environments, just make N copies of the script and create a Sublime Text build system for each.

Unfortunately envs & related versions change constantly, so hard-wiring them is not an option. Also, Linux bash scripts aren't permitted unfortunately.

schlamar wrote:There are already solutions for this issue which are not Sublime specific. Please have a look at virtualenv/virtualenvwrapper. So execute in a virtualenv with virtualenvwrapper in Linux would be as simple as "workon envname && python run $file".

Please note that the syntax in each of your examples is wrong. It should be just like http://blog.pcitron.fr/2013/02/08/multi ... ld-system/. On Linux executing a shell manually and separating commands with "&&", everything should be in one string. On Windows set shell=True and separate commands by "&;".

Yea, that's mostly what I figured, that my syntax is all screw-y...
Looks like this is closest so far, thanks for the tip & link:

"cmd": ["sh", "-c", "setEnv env1 && python run $file"],

unfortunately, for some reason, the shell environment called with that, does not know about my 'usual'/default linux shell environment and the API (setEnv) responsible for setting that, i.e.:
------------return----------------
sh: 1: setEnv: not found
[Finished in 0.2s with exit code 127]
------------------------------------

Back to the poking when I have some time. If anyone has other tips (I'm not too versed with json or linux as you might have figured), everything's more than welcome.
Cheerio.
c.
alec.tron
 
Posts: 5
Joined: Thu May 30, 2013 1:18 am

Re: Python build - dynamic env / multiple cmd

Postby alec.tron on Wed Jun 12, 2013 3:49 am

Eh, t'was the right path you put me on, thanks!.
In case anyone else has similar requirements, after talking to some more Linux savy guys around here, this sh needs to be tcsh:

"cmd": ["tcsh", "-c", "setEnv env1 && python run $file"],

Ta-da.
c.
alec.tron
 
Posts: 5
Joined: Thu May 30, 2013 1:18 am


Return to General Discussion

Who is online

Users browsing this forum: FichteFoll, Google [Bot] and 12 guests