Sublime Forum

Python build - dynamic env / multiple cmd

#1

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 )
sublimetext.userecho.com/topic/8 … ld-system/
Multiple cmd in build system? (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,
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”
0 Likes

#2

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.

0 Likes

#3

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.

0 Likes

#4

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.

#!/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.

0 Likes

#5

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 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 “&;”.

0 Likes

#6

Hey guys,
thanks for the replies!

[quote=“fjl”]
If you need many environments, just make N copies of the script and create a Sublime Text build system for each.[/quote]

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

[quote=“schlamar”]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 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 “&;”.[/quote]

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.

0 Likes

#7

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.

0 Likes