leointeg icon indicating copy to clipboard operation
leointeg copied to clipboard

LeoInteg should support connecting to Leo running in a venv

Open ranvik14 opened this issue 2 years ago • 2 comments

The problem was described in this message on Leo's google group:

  • https://groups.google.com/g/leo-editor/c/rPWY2AxO7cE/m/a-114jfTAAAJ

In a follow-up msg in the same thread a possible solution was described by TBP - See

  • https://groups.google.com/g/leo-editor/c/rPWY2AxO7cE/m/C-Juy9fjAAAJ

@boltex : For simple access here's the text of the proposed solution:

"""I suggest that Felix could find a way to have LeoInteg open a venv and launch Python from that. This would require a new setting that would take priority over the current setting for the Python executable. Leo integ would have to run the venv activation script and launch the bridge in that environment. I don't know how to do that but I imagine it's possible. Once launched, LeoInteg would not need to know about the venv any more to communicate with the bridge."""

In a follow-up comment I'll describe how I am using pip & venv to install Leo from PyPI.

ranvik14 avatar Dec 29 '23 15:12 ranvik14

In all my Linux VMs - either Debian or Fedora based - I'm always using Python's virtual environments in the same way:

  • I have a directory 'PyVE' in my home directory with two sub-direcories:
  • -> Directory # 1 - called ' GitHub' - holding the latest version of 'https://github.com/leo-editor/leo-editor/blob/devel/README.md' - while -
  • -> Directory # 2 - called 'PyPI' - is holding the latest stable version of Leo released at PyPI.

For '~/PyVE/PyPI' I use the following ~ activation ~ steps:

  • cd ~/PyVE/PyPI/Leo
  • source bin/activate
  • leo &

ranvik14 avatar Dec 29 '23 20:12 ranvik14

@ranvik14 Thank you for entering this issue.

I'll take a deeper look into it right after I finish LeoJS 1.0.

boltex avatar Mar 31 '24 23:03 boltex

@tbpassin @ranvik14 Sorry for waiting so long on this issue!

Since I've never used venv for python (anaconda, etc... i guess?) I'm going to need a little guidance from you! 😉

I'll release a new LeoInteg version very soon and I'll gladly add an option to launch a virtual python environment in leoInteg - (I'll also fix a little settings display string where the 'Server Process Launch Command' is shown where the /leoserver.py part is missing after the /leo/core ! Oops! This missing string is particularly silly because it's meant to help the user understand what command will be launched and it's not even representative of what is really launched! )

But first, I'd like to know Would a string similar to this work for the python command? (adapted to your OS and venv location, of course)

cd ~/PyVE/PyPI/Leo && source bin/activate && python

(to be entered in the 'python command' text box of the LeoInteg settings pictured below)

Image

If not, what errors are you getting? what does that produce ?

boltex avatar Jul 12 '25 22:07 boltex

@ranvik14 @tbpassin I guess you would also have to manually adjust the 'Leo-Editor Path' above to the appropriate value. (refer to the resulting 'Server Process Launch Command' below. (that would be missing the last '/leoserver.py' part, as shown in this corrected screenshot of the next LeoInteg version below)

Image

In red is the part that is actually missing from this settings screen but is actually added when launching! I'll fix this in the next version coing soon that will show the actual command used in its entirety properly. for now consider that '/leoserver.py' part is already added to that command, just before the '--port' part.

boltex avatar Jul 12 '25 22:07 boltex

One thing to remember is that once the venv is activated, the version of Python that will be launched by "python3" might change. At least, this is the case on Windows. For example, I installed Python 3.13, but there are no user-installed packages for it except for what's in my leo-313 venv. When I activate the venv, I get:

C:\Users\Tom>\tom\venvs\leo-313\Scripts\activate

(leo-313) C:\Users\Tom>py -V
Python 3.13.3

Note that you don't "source" the activate script in Windows. Outside of an activated venv, on my computer "py" launches Python 3.12.8:

(leo-313) C:\Users\Tom>deactivate
C:\Users\Tom>py -V
Python 3.12.8

I can't check with Linux in a VM right now because some recent update to Windows has broken Virtualbox and I can't launch any VM.

It's possible, I think, to run pyserver with a different version of Python than what Leo is going to run under but that might get confusing. Also the path to the activate script is quite different on WIndows than on Linux, in case that needs to get taken into account.

Just a few ins and outs of venvs for you to think about.

tbpassin avatar Jul 12 '25 23:07 tbpassin

@ranvik14 @tbpassin Thanks for those explanations - it helps a lot

I've just realized chaining commands will not work because I use the 'spawn' node function to start the new process.

Someone suggested I do this in LeoInteg instead :

  • Let the user provide the path to the venv directory (in a new setting text box above 'Python Command')
  • Internally resolve the correct python executable
  • Pass it to spawn() like this:
const venvPath = config.get('leoInteg.venvPath'); // this is the new setting
const pythonPath = path.join(venvPath, isWindows ? 'Scripts' : 'bin', 'python');
const leoEditorPath = config.get('leoInteg.leoEditorPath'); // existing leoEditor path setting
const leoServerScript = path.join(leoEditorPath, 'leoserver.py');
const args = [leoServerScript];
// ... push other args like limit, port, etc...
this._serverProcess = child.spawn(pythonPath, args, options);  // finish with the same spawn as before.

So essentially, this would allow users to run Leo from within their preferred virtual environment, without needing to manually activate it or use shell commands.

LeoInteg would just launch the leoserver.py from the specified Leo installation using the Python interpreter inside the selected venv, like this:

/home/user/my-venv/bin/python C:/prog/leo-editor/leoserver.py

(Both the venv path and the Leo installation path come from settings the user already controls.)

Let me know if you think this direction works for you, I’ll start implementing it. 😄

boltex avatar Jul 13 '25 03:07 boltex

@ranvik14 @tbpassin This long standing issue is finally resolved and working in the dev branch.

It will be available as the main new feature of LeoInteg 1.0.5, to be released in a few hours. 😄

boltex avatar Aug 02 '25 03:08 boltex