Skip to content

Commit 8c9155f

Browse files
mmarchetticfhammill
andauthored
Add ability to override python version (#617)
* add ability to override python version in case the version of python on the deployment machine isn't available on the connect server, attempt to deploy the correct version by patching the environment * add documentation for --override-python-version * add override_python_version to remaining main entrypoints * accept major.minor versions * add missing CLI argument descriptors * add validation --------- Co-authored-by: cfhammill <[email protected]>
1 parent 26d5acb commit 8c9155f

File tree

4 files changed

+160
-22
lines changed

4 files changed

+160
-22
lines changed

README.md

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,6 @@ prepared `manifest.json` file. See ["Deploying R or Other
1212
Content"](#deploying-r-or-other-content) for details.
1313

1414

15-
## Deploying Python Content to Posit Connect
16-
17-
Posit Connect supports the deployment of Jupyter notebooks, Python APIs (such as
18-
those based on Flask or FastAPI) and apps (such as Dash, Streamlit, and Bokeh apps).
19-
Much like deploying R
20-
content to Posit Connect, there are some caveats to understand when replicating your
21-
environment on the Posit Connect server:
22-
23-
Posit Connect insists on matching `<MAJOR.MINOR>` versions of Python. For example,
24-
a server with only Python 3.9 installed will fail to match content deployed with
25-
Python 3.8. Your administrator may also enable exact Python version matching which
26-
will be stricter and require matching major, minor, and patch versions. For more
27-
information see the [Posit Connect Admin Guide chapter titled Python Version
28-
Matching](https://docs.posit.co/connect/admin/python/#python-version-matching).
29-
3015
### Installation
3116

3217
To install `rsconnect-python` from PYPI, you may use any python package manager such as
@@ -233,6 +218,33 @@ ensuring that you use the same Python that you use to run your Jupyter Notebook:
233218
/path/to/python -m pip list --format=freeze
234219
```
235220

221+
#### Python Version
222+
223+
When deploying Python content to Posit Connect,
224+
the server will require matching `<MAJOR.MINOR>` versions of Python. For example,
225+
a server with only Python 3.9 installed will fail to match content deployed with
226+
Python 3.8. Your administrator may also enable exact Python version matching which
227+
will be stricter and require matching major, minor, and patch versions. For more
228+
information see the [Posit Connect Admin Guide chapter titled Python Version
229+
Matching](https://docs.posit.co/connect/admin/python/#python-version-matching).
230+
231+
We recommend installing a version of Python on your client that is also available
232+
in your Connect installation. If that's not possible, you can override
233+
rsconnect-python's detected Python version and request a version of Python
234+
that is installed in Connect, For example, this command:
235+
236+
```bash
237+
rsconnect deploy api --override-python-version 3.11.5 my-api/
238+
```
239+
240+
will deploy the content in `my-api` while requesting that Connect
241+
use Python version 3.11.5.
242+
243+
> **Note**
244+
> The packages and package versions listed in `requirements.txt` must be
245+
> compatible with the Python version you request.
246+
247+
236248
#### Static (Snapshot) Deployment
237249

238250
By default, `rsconnect` deploys the original notebook with all its source code. This
@@ -371,6 +383,32 @@ ensuring that you use the same Python that you use to run your API or applicatio
371383
/path/to/python -m pip list --format=freeze
372384
```
373385

386+
#### Python Version
387+
388+
When deploying Python content to Posit Connect,
389+
the server will require matching `<MAJOR.MINOR>` versions of Python. For example,
390+
a server with only Python 3.9 installed will fail to match content deployed with
391+
Python 3.8. Your administrator may also enable exact Python version matching which
392+
will be stricter and require matching major, minor, and patch versions. For more
393+
information see the [Posit Connect Admin Guide chapter titled Python Version
394+
Matching](https://docs.posit.co/connect/admin/python/#python-version-matching).
395+
396+
We recommend installing a version of Python on your client that is also available
397+
in your Connect installation. If that's not possible, you can override
398+
rsconnect-python's detected Python version and request a version of Python
399+
that is installed in Connect, For example, this command:
400+
401+
```bash
402+
rsconnect deploy api --override-python-version 3.11.5 my-api/
403+
```
404+
405+
will deploy the content in `my-api` while requesting that Connect
406+
use Python version 3.11.5.
407+
408+
> **Note**
409+
> The packages and package versions listed in `requirements.txt` must be
410+
> compatible with the Python version you request.
411+
374412
### Creating a Manifest for Future Deployment
375413

376414
You can create a `manifest.json` file for an API or application, then use that

rsconnect/bundle.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,7 @@ def get_python_env_info(
17321732
file_name: str,
17331733
python: str | None,
17341734
force_generate: bool = False,
1735+
override_python_version: str | None = None,
17351736
) -> tuple[str, Environment]:
17361737
"""
17371738
Gathers the python and environment information relating to the specified file
@@ -1752,6 +1753,9 @@ def get_python_env_info(
17521753
logger.debug("Python: %s" % python)
17531754
logger.debug("Environment: %s" % pformat(environment._asdict()))
17541755

1756+
if override_python_version:
1757+
environment = environment._replace(python = override_python_version)
1758+
17551759
return python, environment
17561760

17571761

@@ -2243,6 +2247,7 @@ def create_python_environment(
22432247
directory: str,
22442248
force_generate: bool = False,
22452249
python: Optional[str] = None,
2250+
override_python_version: Optional[str] = None
22462251
) -> Environment:
22472252
module_file = fake_module_file_from_directory(directory)
22482253

@@ -2253,7 +2258,7 @@ def create_python_environment(
22532258
_warn_if_environment_directory(directory)
22542259

22552260
# with cli_feedback("Inspecting Python environment"):
2256-
_, environment = get_python_env_info(module_file, python, force_generate)
2261+
_, environment = get_python_env_info(module_file, python, force_generate, override_python_version)
22572262

22582263
if force_generate:
22592264
_warn_on_ignored_requirements(directory, environment.filename)

0 commit comments

Comments
 (0)