Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Python - Create a Virtual Environment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ This repository contains 5 custom steps which are offered as examples of how you

## Change Log

* Version 2.1.0 (01SEP2025)
- Converted ORIGINAL_PYPATH to global macro variable to avoid breaking downstream steps
* Version 2.0.0 (26AUG2025)
- **Refactored code to leverage venv (*Goodbye, virtualenv!*)**
- Separate folder in repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
{
"id": "version_text",
"type": "text",
"text": "Version : 2.0.0 (26AUG2025)",
"text": "Version : 2.1.0 (01SEP2025)",
"visible": ""
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* -------------------------------------------------------------------------------------------*
Python - Create a Virtual Environment

v 2.0.0 (26AUG2025)
v 2.1.0 (01SEP2025)

This program helps you create a Python virtual environment from within a SAS session.
It captures the current Python executable path and creates a virtual environment in the
Expand Down Expand Up @@ -53,7 +53,7 @@ from pathlib import Path

# Capture current Python executable and save in a macro variable called ORIGINAL_PYPATH for reference
pyt=os.environ["PROC_PYPATH"]
SAS.symput("ORIGINAL_PYPATH",str(pyt))
SAS.logMessage(f"Current Python Path: {pyt}")

# Create a virtual environment in the directory specified.
venv_input=SAS.symget("venv_input")
Expand Down Expand Up @@ -152,6 +152,15 @@ run;
/*-----------------------------------------------------------------------------------------*
MACROS
*------------------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------------*
The following two macro variable is meant to be global as it refers to a changed state
of the global environment (SAS Studio session) it exists in.
*------------------------------------------------------------------------------------------*/
%global ORIGINAL_PYPATH;



/* -------------------------------------------------------------------------------------------*
Macro to initialize a run-time trigger global macro variable to run SAS Studio Custom Steps.
A value of 1 (the default) enables this custom step to run. A value of 0 (provided by
Expand Down Expand Up @@ -232,6 +241,13 @@ run;

%_create_error_flag(_cvirenv_error_flag, _cvirenv_error_desc);

/*-----------------------------------------------------------------------------------------*
Extract existing Python path
*------------------------------------------------------------------------------------------*/
%if &_cvirenv_error_flag. = 0 %then %do;
%let ORIGINAL_PYPATH=%sysget(PROC_PYPATH);
%end;

/*-----------------------------------------------------------------------------------------*
Extract value from folder selector
*------------------------------------------------------------------------------------------*/
Expand Down
15 changes: 15 additions & 0 deletions Python - Create a Virtual Environment/extras/Test_observations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Testing Bugs encountered

The following is more an internal focussed activity (and perhaps won't / needn't be included). To illustrate bugs or errors that came about during a test of this custom step (in the hope that it promotes awareness / learning)

1. Testing of [Python - Switch Environments](../../Python%20-%20Switch%20Environments/) raised this error, which gives us the lesson that ORIGINAL_PYPATH should be made global.

```sas
399 %put &_switchenv_error_desc.;
ERROR: ORIGINAL_PYPATH does not exist. Cannot revert to original environment.
400 %put NOTE: Error desc if any - &_switchenv_error_desc. ;
NOTE: Error desc if any - ERROR: ORIGINAL_PYPATH does not exist. Cannot revert to original environment.
401

```

2 changes: 2 additions & 0 deletions Python - Switch Environments/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
Test *

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions Python - Switch Environments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

# Python - Switch Environments

## Description
Switch between different Python environments or revert to the original environment from within your SAS Viya session. This SAS Studio custom step enables you to seamlessly toggle between a specified virtual environment (venv) and the default/original Python environment, supporting reproducible and portable analytics workflows.

A quick look:

![Video](./img/Switch_Environments.gif)

## User Interface

Refer to the "About" tab on the step for more details.

### Parameters
This step helps you switch between Python environments. Input arguments required:
1. **What would you like to do?**
- Radio Button
- (Default) Revert to original Python environment
- Switch to specified Python environment (venv)
2. **If switching, provide the path to your virtual environment**
- The full path to the 'venv' folder or the folder containing `/bin/python3`.

## Requirements

1. A SAS Viya 4 environment (last test on monthly release 2025.07) with SAS Studio Flows
2. Python configured with the above environment (preferably using the [SAS Configurator for Open Source](https://go.documentation.sas.com/doc/en/itopscdc/v_016/itopswn/p19hj5ipftk86un1axa51rzr5mxv.htm))

## Installation & Usage

Refer to the [steps](../README.md#getting-started---making-a-custom-step-from-this-repository-available-in-sas-studio) listed in the main README.md

## The WHY : Background information

Refer this [blog](https://blogs.sas.com/content/subconsciousmusings/2022/05/16/python-a-la-carte) for background. The ability to create and use virtual Python environments for use within SAS Viya helps data scientists create portable solutions, maintain solution integrity, and exploit the integration between SAS and Python to the fullest extent.

Watch this example!

[SAS & Open Source (Python) Integration : Better Together](https://www.youtube.com/watch?v=YVaX-A-ZsQ0&list=PLpe69msCs2C8IcarG0aEs_iKy4gyRSFPN&index=3)

[Creating virtual Python environments within SAS Studio.](https://youtu.be/UIYZf2bKcWw)

This repository contains custom steps which are offered as examples of how you could create, activate, switch between, and package virtual Python environments from within SAS Viya applications and tools, such as SAS Studio. It makes use of [Custom Steps](https://go.documentation.sas.com/doc/en/webeditorcdc/v_006/webeditorug/n0b7ljqhka8lh5n12judc27x5gph.htm), a component within SAS Studio which helps users package repeatable steps in a user-friendly manner.

## Change Log

* Version 1.0.0 (29AUG2025)
- Initial release: Switch between original and specified Python environments
- Deprecates existing Activate a Virtual Environment and Revert to Original Environment steps under Python Virtual Environments.
- UI for selecting revert or switch, and specifying venv path
- Error handling for missing or invalid venv paths
- Retains and restores original Python path for session portability

## Created / Contact
- Sundaresh Sankaran ([email protected])



Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"showPageContentOnly": true,
"pages": [
{
"id": "param",
"type": "page",
"label": "Parameters",
"children": [
{
"id": "revert_to_original",
"type": "radiogroup",
"label": "What would you like to do?",
"items": [
{
"value": "1",
"label": "Revert to original Python environment"
},
{
"value": "0",
"label": "Switch to specified Python environment(venv)"
}
],
"visible": ""
},
{
"id": "venv",
"type": "path",
"label": "Provide a path to your virtual environment (the 'venv' folder or the folder containing /bin/python3):",
"pathtype": "folder",
"placeholder": "",
"required": false,
"visible": [
"$revert_to_original",
"=",
"0"
],
"enabled": [
"$revert_to_original",
"=",
"0"
]
},
{
"id": "text1",
"type": "text",
"text": "If you choose to revert, the Python environment will be set to the original or default environment used by SAS Studio. If you choose to switch, you must provide the full path to the virtual environment you wish to use.",
"visible": ""
}
]
},
{
"id": "about",
"type": "page",
"label": "About",
"children": [
{
"id": "about_text",
"type": "text",
"text": "Python - Switch Environments\n===============================\n\nThis custom step allows you to switch between different Python environments from within a SAS session, or revert to the original environment.\n\nFor more details, refer to the documentation or contact the author.",
"visible": ""
},
{
"id": "contact_details",
"type": "section",
"label": "Created / Contact",
"open": true,
"visible": "",
"children": [
{
"id": "created_contact_text",
"type": "text",
"text": "Sundaresh Sankaran ([email protected])",
"visible": ""
}
]
},
{
"id": "about_version",
"type": "section",
"label": "Version",
"open": true,
"visible": "",
"children": [
{
"id": "version_text",
"type": "text",
"text": "Version : 1.0.0 (29AUG2025)",
"visible": ""
}
]
}
]
}
],
"syntaxversion": "1.3.0",
"values": {
"revert_to_original": {
"value": "1",
"label": "Revert to original Python environment"
},
"venv": ""
}
}
Loading