CEXT-Dan / PyRx

Python for AutoCAD
41 stars 7 forks source link

Python virtual environment #1

Closed gswifort closed 1 month ago

gswifort commented 6 months ago

Hi, I have a question: at what point does PyRx decide on the choice of a specific python interpreter? During installation or when loading into AutoCAD? I would like to run Autocad from a python virtual environment with a separate interpreter (e.g. .\venv\Scripts\python.exe), but PyRx in Autocad is always set to the global interpreter.

gswifort commented 4 months ago

Going back to the ini file:

I copied these 4 files to D:\GSWI\Documents\pyrx\ image in the ini file I have:

[PYRXSETTINGS]
; ignored if this exact path is already found in env
PYTHONINSTALLEDPATH =c:\users\gswi\appdata\local\programs\python\python312

; this is the path to wxPython
WXPYTHONPATH =c:\users\gswi\appdata\local\programs\python\python312\Lib\site-packages\wx

; this is the path to the auto complete stubs and activex modules
PYRXSTUBPATH =C:\Users\gswi\AppData\Local\Programs\PyRx\Stubs

;uses PyConfig_InitIsolatedConfig
PYTHONISOLATED=0

;applied config.executable ignored if isolaed is 0
PYTHONEXECUTABLE = D:\GSWI\Documents\pyrx\venv\Scripts\python.exe                                                               

in autocad I load D:\GSWI\Documents\pyrx\RxLoader24.1.arx then I load module:

import PyRx as Rx # isort:skip
import PyGe as Ge # isort:skip
import PyGi as Gi # isort:skip
import PyDb as Db # isort:skip
import PyAp as Ap # isort:skip
import PyEd as Ed # isort:skip

def PyRxCmd_doit():
     import sys

     print(sys.executable)

and after calling DOIT I get C:\Program Files\Autodesk\AutoCAD 2022\acad.exe instead of D:\GSWI\Documents\pyrx\venv\Scripts\python.exe

gswifort commented 4 months ago

And if I understand this document correctly, setting sys.executable correctly should take care of most of the configuration for the virtual environment.

CEXT-Dan commented 4 months ago

PYTHONISOLATED should be 1

gswifort commented 4 months ago

Sorry, I didn't add that I tested for PYTHONISOLATED 0 and 1, it always sets sys.executable to acad.exe.

CEXT-Dan commented 4 months ago

[PYRXSETTINGS] PYTHONINSTALLEDPATH =c:\users\dan\appdata\local\programs\python\python312 WXPYTHONPATH =C:\path\to\myenv\Lib\site-packages\wx PYRXSTUBPATH =M:\Dev\Projects\PyRxGit\PyRxStubs PYTHONISOLATED=1 PYTHONEXECUTABLE=C:\path\to\myenv\Scripts\python.exe

def PyRxCmd_doit():
    try:
        print(sys.executable)
    except Exception as err:
        traceback.print_exception(err)

env

CEXT-Dan commented 4 months ago

I probably have the search order messed up

CEXT-Dan commented 4 months ago

Try renaming the .INI in the install folder, you may get a warning messages

gswifort commented 4 months ago

Still nothing.

image

image

image

image

CEXT-Dan commented 4 months ago

Ok, will have to do some sort of logging to sort this out you are running v1.3.009 yes?

I have confidence that this will work, basically, If PYTHONISOLATED=1 and PYTHONEXECUTABLE is a valid path

static bool initIsolated()
{
    PyConfig config;
    PyConfig_InitIsolatedConfig(&config);

    auto [es, venv_executable] = PyRxINI::pythonvenv_path();
    if (es == false)
        return false;

    auto status = PyConfig_SetString(&config, &config.executable, venv_executable.c_str());
    if (PyStatus_Exception(status))
    {
        PyConfig_Clear(&config);
        acutPrintf(_T("\nPyConfig_SetString failed %ls: "), __FUNCTIONW__);
        return false;
    }

    status = Py_InitializeFromConfig(&config);
    PyConfig_Clear(&config);
    if (PyStatus_Exception(status))
    {
        acutPrintf(_T("\nInitializeFromConfig failed %ls: "), __FUNCTIONW__);
        return false;
    }
    return true;
}
gswifort commented 4 months ago

Yes, 1.3.009

CEXT-Dan commented 4 months ago

I’ve uploaded a new version (v1.3.10) to test at your convenience It has a bit of logging, hopefully will shed some light on where the issue is.

You will have to turn it on from lisp and restart CAD (setenv "PYRX_LOG" "1") on (setenv "PYRX_LOG" "0") off

I also noticed wx path may be incorrect. I assume every venv should have it’s own copy

c:\users\gswi\appdata\local\programs\python\python312\Lib\site-packages\wx should be: D:\GSWI\Documents\pyrx\venv\Lib\site-packages\wx

Reading VIRTUAL_ENV should work now, but assumes a default folder structure. With this option, .INI is skipped

CEXT-Dan commented 4 months ago

@gswifort is this design platform-independant? yes, it should work on BricsCAD as well

gswifort commented 4 months ago
  1. Standard installation and .arx files in the project directory: (Failure)

    Polecenie: _APPLOAD
    Wczytano pomyślnie RxLoader24.1.arx.
    
    Polecenie:
    PyRxLoader::getPythonVenvPath
    PyRxLoader::thisModulePath d:\gswi\documents\pyrx\
    PyRxLoader::getInstallPath C:\Users\gswi\AppData\Local\Programs\PyRx
    PyRxLoader::getIniPath d:\gswi\documents\pyrx\PyRx.INI
    PyRxLoader::validateINIPythonInstallPath c:\users\gswi\appdata\local\programs\python\python312
    PyRxLoader::validateINIwxPythonPath c:\users\gswi\appdata\local\programs\python\python312\Lib\site-packages\wx
    
    Loading PyRx from ini condition
    PyRxLoader::PyRxLoader_loader Loading, C:\Users\gswi\AppData\Local\Programs\PyRx\Bin\PyRx24.1.arx  # !!!!!
    
    Finished PyRxLoader_loader
    
    PyRx version <1.3.011.20240809> loaded:
    Python Interpreter Loaded successfully!
    Określ przeciwny narożnik lub [KRAwędź/OWbok/Zwbok]:
    Polecenie: PYLOAD
    
    Success module DEMO is loaded:
    Polecenie:
    Polecenie: DOIT
    C:\Program Files\Autodesk\AutoCAD 2022\acad.exe
  2. Changed name of the installation directory: (It works !)

    Polecenie: _APPLOAD
    Wczytano pomyślnie RxLoader24.1.arx.
    
    Polecenie:
    PyRxLoader::getPythonVenvPath
    PyRxLoader::thisModulePath d:\gswi\documents\pyrx\
    PyRxLoader::getInstallPath C:\Users\gswi\AppData\Local\Programs\PyRx
    PyRxLoader::getIniPath d:\gswi\documents\pyrx\PyRx.INI
    PyRxLoader::validateINIPythonInstallPath c:\users\gswi\appdata\local\programs\python\python312
    PyRxLoader::validateINIwxPythonPath c:\users\gswi\appdata\local\programs\python\python312\Lib\site-packages\wx
    
    Loading PyRx from ini condition
    PyRxLoader::PyRxLoader_loader Loading, d:\gswi\documents\pyrx\PyRx24.1.arx
    
    Finished PyRxLoader_loader
    
    PyRx version <1.3.011.20240809> loaded:
    
    *****Error importing the wxPython API!*****:
    Python Interpreter Loaded successfully!
    PYLOAD
    
    Success module DEMO is loaded:
    Polecenie: DOIT
    D:\GSWI\Documents\pyrx\venv\Scripts\python.exe
    # sys.path ↓
    c:\users\gswi\appdata\local\programs\python\python312\python312.zip
    c:\users\gswi\appdata\local\programs\python\python312\DLLs
    c:\users\gswi\appdata\local\programs\python\python312\Lib
    C:\Users\gswi\AppData\Local\Programs\Python\Python312
    D:\GSWI\Documents\pyrx\venv
    D:\GSWI\Documents\pyrx\venv\Lib\site-packages
    D:\GSWI\Documents\pyrx\venv\Lib\site-packages\pip\_internal
    C:\Users\gswi\AppData\Local\Programs\PyRx\Stubs
    d:\gswi\documents\pyrx\

This probably means that you first look for .ini in the installation directory and then in the local directory instead of the other way around.

  1. Virtual environment activated and global loaders used: (It works !)

    PyRxLoader::getPythonVenvPath D:\GSWI\Documents\pyrx\venv
    PyRxLoader::thisModulePath c:\programdata\autodesk\applicationplugins\pyrx.bundle\contents\
    PyRxLoader::getInstallPath C:\Users\gswi\AppData\Local\Programs\PyRx
    PyRxLoader::getIniPath C:\Users\gswi\AppData\Local\Programs\PyRx\Bin\PyRx.INI
    
    Loading PyRx from venv condition
    D:\GSWI\Documents\pyrx\venv\Lib\site-packages\wx
    PyRxLoader::PyRxLoader_loader Loading, C:\Users\gswi\AppData\Local\Programs\PyRx\Bin\PyRx24.1.arx
    
    Finished PyRxLoader_loader
    
    PyRx version <1.3.011.20240809> loaded:
    
    *****Error importing the wxPython API!*****:
    Python Interpreter Loaded successfully!
    Określ przeciwny narożnik lub [KRAwędź/OWbok/Zwbok]:
    Polecenie: PYLOAD
    
    Success module DEMO is loaded:
    Polecenie: DOIT
    D:\GSWI\Documents\pyrx\venv\Scripts\python.exe
    c:\users\gswi\appdata\local\programs\python\python312\python312.zip
    c:\users\gswi\appdata\local\programs\python\python312\DLLs
    c:\users\gswi\appdata\local\programs\python\python312\Lib
    C:\Users\gswi\AppData\Local\Programs\Python\Python312
    D:\GSWI\Documents\pyrx\venv
    D:\GSWI\Documents\pyrx\venv\Lib\site-packages
    D:\GSWI\Documents\pyrx\venv\Lib\site-packages\pip\_internal
    C:\Users\gswi\AppData\Local\Programs\PyRx\Stubs
    c:\users\gswi\appdata\local\programs\pyrx\bin\
    D:\GSWI\Documents\pyrx\
CEXT-Dan commented 4 months ago

Yay Progress,

The search order is next to the module, but since there’s no .INI in the bundle, it looks to the install You might delete the bundle and have a try to see the behavior, or put a .INI in the bundle

I’ll have to reconsider the paths in that case

schoeller commented 4 months ago

Sorry to bother. Not getting there. Have undertaken the following steps

1#Fresh install of Python 3.12.3 on Win11 2#Opened a folder in VS Code and added a virtual env in the terminal

python -m venv .venv
.\.venv\Scripts\activate
pip install debugpy
pip install wxpython

3#Altered PyRX.INI as follows

[PYRXSETTINGS]
PYTHONINSTALLEDPATH =c:\users\sebastian schoeller\appdata\local\programs\python\python312
WXPYTHONPATH =C:\Users\Sebastian Schoeller\Documents\GitHub\PyDevBox\.venv\Lib\site-packages\wx
PYRXSTUBPATH =C:\Users\Sebastian Schoeller\AppData\Local\Programs\PyRx\Stubs
PYTHONISOLATED=1
PYTHONEXECUTABLE =C:\Users\Sebastian Schoeller\Documents\GitHub\PyDevBox\.venv\Scripts\python.exe

4#Called (setenv "PYRX_LOG" "1") on BCAD 5#Restarting BCAD I get

PyRxLoader::getPythonVenvPath 
PyRxLoader::thisModulePath C:\Users\Sebastian Schoeller\AppData\Local\Programs\PyRx\Bin\
PyRxLoader::getInstallPath C:\Users\Sebastian Schoeller\AppData\Local\Programs\PyRx
PyRxLoader::getIniPath C:\Users\Sebastian Schoeller\AppData\Local\Programs\PyRx\Bin\PyRx.INI
PyRxLoader::validateINIPythonInstallPath c:\users\sebastian schoeller\appdata\local\programs\python\python312
PyRxLoader::validateINIwxPythonPath C:\Users\Sebastian Schoeller\Documents\GitHub\PyDevBox\.venv\Lib\site-packages\wx
Loading PyRx from ini condition
PyRxLoader::PyRxLoader_loader Loading, C:\Users\Sebastian Schoeller\AppData\Local\Programs\PyRx\Bin\PyRxV24.0.brx
Finished PyRxLoader_loader
PyRx version <1.3.015.20240826> loaded:
PYTHONEXECUTABLE failed initIsolated: 
initIsolated failed, trying Py_Initialize  WxRxApp::Init_wxPython: Python Interpreter Loaded successfully!

6#Path to python.exe exists 7#Below settings work

PYTHONINSTALLEDPATH =C:\Users\Sebastian Schoeller\Documents\GitHub\PyDevBox\.venv\Scripts
PYTHONISOLATED=0
CEXT-Dan commented 3 months ago

I’ll have to see if I can get an error message from Python as to what went wrong Py_InitializeFromConfig returned the error

CEXT-Dan commented 3 months ago

I think I see the error, I will test this this afternoon

CEXT-Dan commented 3 months ago

Actually, turned out to be an issue with BRX, I’m testing a workaround, filed SR 176582

gswifort commented 3 months ago

When using a virtual environment, sys.prefix is set to the <venv> directory, I suggest changing it to sys.base_prefix, it will work in both cases.

https://github.com/CEXT-Dan/PyRx/blob/a461f0025b1c2e7a708b9f34ec4459df42d0d128/PyRxStubs/PyRxDebug.py#L27

CEXT-Dan commented 3 months ago

Thanks! I add that

github-actions[bot] commented 1 month ago

Stale issue message