Virtual Environments and 3ds Max Python
3ds Max 2021 and higher supports using virtual environments with Python 3, which allows you to ensure specific Python modules are loaded. This can be very useful for tools that have version-specific dependencies, and you need them to run in controlled environments. When a virtual environment is active, the Python interpreter in 3ds Max will search it first for imported modules - it becomes the first path on sys.path
.
To set up a virtual environment, you need to create it outside of the 3ds Max application, using the 3ds Max Python interpreter, and then start 3ds Max from the same command prompt. As of 3ds Max 2022, the venv
module is included in the Python distribution. In versions before 3ds Max 2022, you will need to install virtualenv
. To start 3ds Max with a virtual environment:
- Open a command prompt.
- In versions before 3ds Max 2022, install the
virtualenv
module using the 3ds Max Python interpreter:C:\Program Files\Autodesk\3ds Max 2021\Python37\python.exe -m pip install --user virtualenv
Note See "Using pip with Python 3" in Python extension libraries for instructions on how to install
pip
.
For 3ds Max 2022 and later, the process for creating and using a virtual environment work identically for both virtualenv
and venv
. Substitute venv
for virtualenv
if required:
Create a new virtual environment (this only needs to be done once):
C:\Program Files\Autodesk\3ds Max 2023\Python\python.exe -m virtualenv d:\myenv
Change the current directory to the virtual environment's script directory, and run the activate script (this needs to be done every time you start 3ds Max):
cd d:\myenv\Scripts PS D:\myenv\Scripts> .\activate.bat
The prompt changes to indicate that the virtual environment is active.
Note: You can deactivate the virtual environment using the
deactivate.bat
script in the same directory. You can view the path for any active virtual environment by usingwhere python
in the command prompt. Once the environment is activated, you can install the specific modules you require.
For example:pip install pip_install_test
Start 3ds Max from the same command line:
<3dsmax_install_path>\3dsmax.exe
Note: 3ds Max must be started in the same command prompt that ran
activate.bat
, as that script affects the local environment variables.The environment is now active in 3ds Max, and you can import and use installed modules. You can test that the environment is activated in 3ds Max by checking the
VIRTUAL_ENV
environment variable, which is set to the path of the environment created above.
For more general information about using a virtual environment, see the Python documentation for Installing Packages using pip and virtual environments.