Skip to main content

pytask-stata

PyPI PyPI - Python Version image image PyPI - License image image pre-commit.ci status Ruff


Run Stata's do-files with pytask.

Installation

pytask-stata is available on PyPI and Anaconda.org. Install it with

$ uv add pytask-stata

# or

$ pixi add pytask-stata

You also need to have Stata installed on your system and have the executable on your system's PATH. If you do not know how to do it, here is an explanation.

Usage

Similarly to normal task functions which execute Python code, you define tasks to execute scripts written in Stata with Python functions. The difference is that the function body does not contain any logic, but the decorator tells pytask how to handle the task.

Here is an example where you want to run script.do.

from pathlib import Path

from pytask import mark


@mark.stata(script=Path("script.do"))
def task_run_do_file(produces: Path = Path("auto.dta")):
    pass

When executing a do-file, the current working directory changes to the directory where the task module is located. This allows you, for example, to reference data sets with a relative path from the task module.

Dependencies and Products

Dependencies and products can be added as with a normal pytask task using the task function signature as explained in this tutorial.

Here is a task with one dependency and one product.

from pathlib import Path

from pytask import mark


@mark.stata(script=Path("script.do"))
def task_run_do_file(
    depends_on: Path = Path("input.dta"),
    produces: Path = Path("auto.dta"),
):
    pass

pytask uses input.dta and auto.dta to decide whether the task needs to run. If input.dta changes or auto.dta is missing, the Stata task is executed again.

You can also use @task(kwargs=...) to define dependencies which are not part of the function signature.

from pathlib import Path

from pytask import mark
from pytask import task


@task(kwargs={"depends_on": Path("input.dta")})
@mark.stata(script=Path("script.do"))
def task_run_do_file(produces: Path = Path("auto.dta")):
    pass

Accessing dependencies and products in the script

Dependencies and products registered in the task function signature are used by pytask to order tasks and track whether they are up-to-date. They are not automatically passed to the Stata script. Use the options argument of the decorator to pass paths or other values as command line arguments to your Stata executable.

For example, pass paths for the dependency and product with

from pathlib import Path

from pytask import mark


@mark.stata(script=Path("script.do"), options=[Path("input.dta"), Path("auto.dta")])
def task_run_do_file(
    depends_on: Path = Path("input.dta"),
    produces: Path = Path("auto.dta"),
):
    pass

And in your script.do, you can intercept the value with

* Intercept command line arguments and save them to macros.
args depends_on produces

sysuse auto, clear
save "`produces'"

The relative path inside the do-file works only because pytask-stata switches the current working directory to the directory of the task module before the task is executed.

To make the task independent from the current working directory, pass the full path as an command line argument. Here is an example.

# Absolute path to the build directory.
from pathlib import Path

from pytask import mark

from src.config import BLD


@mark.stata(script=Path("script.do"), options=BLD / "auto.dta")
def task_run_do_file(produces: Path = BLD / "auto.dta"):
    pass

Repeating tasks with different scripts or inputs

You can also parametrize the execution of scripts, meaning executing multiple do-files as well as passing different command line arguments to the same do-file.

The following task executes two do-files which produce different outputs.

from pathlib import Path

from pytask import mark
from pytask import task

for i in range(2):

    @task
    @mark.stata(script=Path(f"script_{i}.do"), options=f"{i}.dta")
    def task_execute_do_file(produces: Path = Path(f"{i}.dta")):
        pass

Configuration

pytask-stata can be configured with the following options.

stata_keep_log

Use this option to keep the .log files which are produced for every task. This option is useful to debug Stata tasks. Set the option via the configuration file with

[tool.pytask.ini_options]
stata_keep_log = true

The option is also available in the command line interface via the --stata-keep-log flag.

stata_check_log_lines

Use this option to vary the number of lines in the log file which are checked for error codes. It also controls the number of lines displayed on errors. Use any integer greater than zero. Here is the entry in the configuration file

[tool.pytask.ini_options]
stata_check_log_lines = 10

and here via the command line interface

$ pytask build --stata-check-log-lines 10

Changes

Consult the release notes to find out about what is new.

Metadata

Release files for pytask-stata 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytask-stata 0.5.1
File Size Uploaded
pytask_stata-0.5.1.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytask-stata 0.5.1
File Interpreter ABI Platform
pytask_stata-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.2 kB

Release files / pytask_stata-0.5.1.tar.gz

Download URL pytask_stata-0.5.1.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ab1d0e9294fc1303dc0e0cdc5e349a293d6abc7330c2fa7dd12fa91d62ef7423
BLAKE2b-256 checksum
How to use checksums
6117681178ef0f46414df53ef7af1831b4abf20df2c242d2260653b6166834a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pytask_stata-0.5.1-py3-none-any.whl

Download URL pytask_stata-0.5.1-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d99e9b2474c59c140abfa9062826b5e794dbf7d6c8ae1a4daaaad9b11ede0e12
BLAKE2b-256 checksum
How to use checksums
9fc64416def635538f39efc2e3ccfa4521d0559cae1f41f780888fff884de96c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

0.6.0

2 release files

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page