Using uv to run Python tools without installing them

Posted on Thu 23 January 2025 in python, uv • Tagged with python, uv

I wanted to run a Python tool without installing it, or preparing a virtual environment and all that. This is now very easy with uv, using uvx.

I wanted to run markitdown, a tool to convert various files to Markdown. I didn't want to install it, so I used uvx:

uvx markitdown "file_to_convert.docx" > output_file.md

It downloads the tool and installs it in a temporary, isolated environment.

A tool can be installed with uv tool install, like this:

uv tool install markitdown

It's very convenient.


Serving JavaScript with Python's http.server

Posted on Wed 19 July 2023 in Python, JavaScript, Web • Tagged with Python, JavaScript, Web

I was trying to serve a JavaScript file with Python's http.server module, but I was getting this error:

Loading module from http://localhost:9876/main.js” was blocked because of a disallowed MIME type (“text/plain”).

I saw in this StackOverflow answer that I needed to change the MIME type for JavaScript files. I'm using Windows, so I checked the key HKEY_CLASSES_ROOT\.js in RegEdit, and I found that it had this value:

(Default)     JSFile
ContentType   text/plain
PerceivedType text

I changed the ContentType to application/javascript, and now my JavaScript files are served with the correct MIME type.

I'm not sure why the MIME type was set to text/plain in the first place. I'm guessing that it was set by a program that I installed, but I don't know which one. I'm also not sure if changing the MIME type will cause any problems with other programs.


Pylance and mypy cannot find editable local imports

Posted on Thu 01 June 2023 in Pylance, Mypy, Python • Tagged with Pylance, Mypy, Python

The other day, I wrote a post about how I fixed a problem with Pylance not finding a local import. It worked, but now mypy was complaining about the same import.

The solution was installing the package in compat mode with:

pip install -e . --config-settings editable_mode=compat

Reading units from a pickle file with Pint

Posted on Tue 30 May 2023 in Pylance, Python, Pint • Tagged with Pylance, Python, Pint

I have a Python package called cloudmodel that uses Pint to define units and adds some to the default registry. When I read a pickle file that use these units, I get an error:

int.errors.UndefinedUnitError: 'usd' is not defined in the unit registry

The way to fix it is to set the application registry to the one used by cloudmodel:

from cloudmodel.unified.units import ureg
from pint import set_application_registry

set_application_registry(ureg)

Pylance cannot find editable local import

Posted on Sat 27 May 2023 in Pylance, Python • Tagged with Pylance, Python

I had a problem with Pylance not finding a local import. I had installed the package in editable mode with pip install -e . and it was working fine in the terminal, but Pylance was not able to find it.

The solution was installing the package in strict mode with:

pip install -e . --config-settings editable_mode=strict

I got there from this troubleshooting page and this explanation in the Setuptools documentation.