-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python virtual environment #1
Comments
Hi, Currently, nothing is implemented to handle virtual environments. Each instance of AutoCAD fires up a new interpreter by calling Py_Initialize(), The module loads python312.dll into the AutoCAD process, python.exe is never called I’m not entirely sure how to handle virtual environments in an embedded context, but I will look into this |
Thanks for the answer.
|
Hi do you mean a new .DWG, as in a side database? import traceback import PyRx as Rx import PyGe as Ge import PyGi as Gi import PyDb as Db import PyAp as Ap import PyEd as Ed #scope so everything is closed def processDb(db : Db.Database): line = Db.Line(Ge.Point3d(0,0,0),Ge.Point3d(100,100,0)) line.setDatabaseDefaults(db) model = Db.BlockTableRecord(db.modelSpaceId(), Db.OpenMode.kForWrite) model.appendAcDbEntity(line) def PyRxCmd_doit() -> None: try: sideDb = Db.Database() processDb(sideDb) sideDb.saveAs("e:\\newdwg.dwg") except Exception as err: traceback.print_exception(err) |
Yes, that was it, thank you! Coming back to the virtual environment, |
I think I found the solution, just set |
Great! Note: I’m not happy with the current installer, I don’t want to have to write anything to the registry. This loader module will expose PyConfig, https://docs.python.org/3/c-api/init_config.html#init-path-config , I’m not sure the format yet, maybe an .INI or XML configuration file that sits in the same folder. This is some time off though, as I’m still writing wrappers |
Yes, this should also solve the virtual environment issue (take into account the |
@gswifort {
"version": "0.2.0",
"configurations": [
{
"name": "Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "127.0.0.1",
"justMyCode": false,
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
"env": {
"PYTHONPATH": "$PYTHONPATH:C:/ProgramData/Autodesk/ApplicationPlugins/PyRx.bundle/Contents/PyRxStubs"
},
}
]
} VS Code states that env property is not allowed. I have tried adding Any hints? Best regards Sebastian |
I have the following debugging settings: launch.json {
"name": "Autocad attach",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "127.0.0.1",
"port": 5678
},
"justMyCode": false
} settings.json {
"python.envFile": "${workspaceFolder}/.env"
} .env
However, the error you show will not be resolved by setting the PYTHONPATH to the "stubs" folder. The "stubs" folder contains only |
Not quite sure how to deal with that vscode warning. The problem is PyLance can’t see python embedded inside C++ |
If the problem is only the display of errors, you can simply ignore them, as follows: import PyRx as Rx # type:ignore
import PyGe as Ge # type:ignore
import PyGi as Gi # type:ignore
import PyDb as Db # type:ignore
import PyAp as Ap # type:ignore
import PyEd as Ed # type:ignore If you use isort then also: import PyRx as Rx # isort:skip # type:ignore
import PyGe as Ge # isort:skip # type:ignore
import PyGi as Gi # isort:skip # type:ignore
import PyDb as Db # isort:skip # type:ignore
import PyAp as Ap # isort:skip # type:ignore
import PyEd as Ed # isort:skip # type:ignore |
Thanks for the work. My current environment foresees the following working steps 1#Start BCAD and VS Code in parallel Thus all modules must be installed accesible to the interpreter in BCAD (which is system-wide Python). VDEV is only available to VS Code. Is there any chance that I may have missed something and may directly launch and debug from within VS Code or reorganize my Python installation? Best Sebastian |
For BricsCad to see the virtual environment (installed packages), the directory
and run BricsCad from the console with the virtual environment activated (for Autocad it is
>>> PYCMDPROMPT
>>> import sys
>>> sys.path.insert(0, ".\<virtualenv>\Lib\site-packages") |
Some ideas: -create loader modules for each host application. -the installer will install main bulk of the package, i.e. wrappers, stubs, samples -Only the AutoCAD loader modules and PackageContents.xml will be installed in ApplicationPlugins
|
What do you mean by loader module? |
Another .ARX module that loads into Autocad, I'll need to hook into Autocads DLL paths and add the necessary paths |
Hi, I’ve added these functions: But I don’t quite understand how they are used, but the goal is to be able to add configurations to the INI, that I can pass along. |
Hi, Wednesday at the earliest. |
Hi, I don't know how to use what you did. |
Yeah, It clear I don’t understand how all this works. “From what I see, I can use PyRx.ini, but only globally, this file is not searched in the current directory (virtual environment).” It’s not meant to be, the design is to tell AutoCAD the path to the DLLs. And to pass information to How about we start with: I’ll pass these to Py_InitializeFromConfig |
Yes, it is possible that this will be enough.
use the file located in this directory (we assume that the user may have different versions of python installed). |
@gswifort is this design platform-independant? |
I don't have access to macOS to test, but it seems so. PEP 587 |
Analyzing the python venv module also shows that it is platform-independent |
If you run AutoCAD from the console (type |
Acad didn’t launch for me, but it sounds reasonable that would be the case. |
it sees it
|
Going back to the I copied these 4 files to [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
and after calling |
And if I understand this document correctly, setting |
PYTHONISOLATED should be 1 |
Sorry, I didn't add that I tested for |
[PYRXSETTINGS] def PyRxCmd_doit():
try:
print(sys.executable)
except Exception as err:
traceback.print_exception(err) |
I probably have the search order messed up |
Try renaming the .INI in the install folder, you may get a warning messages |
Ok, will have to do some sort of logging to sort this out I have confidence that this will work, basically, 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;
} |
Yes, 1.3.009 |
I’ve uploaded a new version (v1.3.10) to test at your convenience You will have to turn it on from lisp and restart CAD 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 Reading VIRTUAL_ENV should work now, but assumes a default folder structure. |
|
This probably means that you first look for
|
Yay Progress, The search order is next to the module, but since there’s no .INI in the bundle, it looks to the install I’ll have to reconsider the paths in that case |
Sorry to bother. Not getting there. Have undertaken the following steps 1#Fresh install of Python 3.12.3 on Win11
3#Altered PyRX.INI as follows
4#Called
6#Path to
|
I’ll have to see if I can get an error message from Python as to what went wrong |
I think I see the error, I will test this this afternoon |
issue #1 fix pythonvenv_path for BricsCAD acedGetEnv bug
Actually, turned out to be an issue with BRX, I’m testing a workaround, filed SR 176582 |
When using a virtual environment, Line 27 in a461f00
|
Thanks! I add that |
Stale issue message |
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.The text was updated successfully, but these errors were encountered: