Skip to content

Conversation

@remlapmot
Copy link
Contributor

@remlapmot remlapmot commented Oct 24, 2025

Hi Tim

I have had a go at this.

I used your ChatGPT suggestions.

I have tested on the most recent versions of Python 3.10, 3.11, 3.12, and 3.13 and they all pass nbdev_test (as you can see from the GitHub Actions workflow I've amended) and a quarto render of a test Quarto document.

However, I have a mysterious problem. When I run this under the recently released Python 3.14.0 I obtain some sort of problem originating from within Stata's pystata. Weirdly nbdev_test passes. But when I run a test quarto render the kernel hangs at the first Stata chunk. To see more I converted my test qmd file to a Jupyter notebook.

First create a UV virtual environment with Python 3.14.0 and activate it

uv venv --python 3.14.0
source .venv/bin/activate

Then add the packages in the pyproject.toml and nbstata

uv sync

Install the kernel

python -m nbstata.install

Then this will hang, where my test.qmd is simply

---
jupyter: nbstata
format:
  html:
    embed-resources: true
---

```{stata}
about
```

```{stata}
twoway function x^2
```
quarto render test.qmd

Kill that with Ctrl+C and convert qmd file to ipynb

quarto convert test.qmd

Then open the ipynb in JupyerLab

jupyter lab test.ipynb

Then executing a stata chunk, the error I obtain is:

[I 2025-10-24 19:55:21.654 ServerApp] Connecting to kernel bbee13b3-64d2-4006-baf2-5ade854c5cbb.
[IPKernelApp] ERROR | Exception in message handler:
Traceback (most recent call last):
  File "/Users/tom/Documents/GitHub/nbstata/.venv/lib/python3.14/site-packages/ipykernel/kernelbase.py", line 469, in dispatch_shell
    await result
  File "/Users/tom/Documents/GitHub/nbstata/.venv/lib/python3.14/site-packages/ipykernel/ipkernel.py", line 379, in execute_request
    await super().execute_request(stream, ident, parent)
  File "/Users/tom/Documents/GitHub/nbstata/.venv/lib/python3.14/site-packages/ipykernel/kernelbase.py", line 896, in execute_request
    reply_content = self.do_execute(**do_execute_args)
  File "/Users/tom/Documents/GitHub/nbstata/nbstata/kernel.py", line 166, in do_execute
    code_cell.run()
    ~~~~~~~~~~~~~^^
  File "/Users/tom/Documents/GitHub/nbstata/nbstata/cell.py", line 29, in run
    self.stata_session.dispatch_run(self.code,
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
        quietly=self.quietly, echo=self.echo, noecho=self.noecho)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/Documents/GitHub/nbstata/nbstata/stata_session.py", line 185, in dispatch_run
    run_noecho(code, self.sc_delimiter, run_as_prog=self._run_as_program_w_locals)
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/Documents/GitHub/nbstata/nbstata/noecho.py", line 88, in run_noecho
    run_non_prog_noecho(block['std_code'], run_as_prog=run_as_prog)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/Documents/GitHub/nbstata/nbstata/noecho.py", line 65, in run_non_prog_noecho
    run_direct(valid_single_line_code(std_non_prog_code),
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               quietly=False, inline=True, echo=False)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/Documents/GitHub/nbstata/nbstata/stata.py", line 68, in run_direct
    return pystata.stata.run(cmds, quietly, echo, inline)
           ^^^^^^^^^^^^^
AttributeError: module 'pystata' has no attribute 'stata'

Would you have any ideas? I might ask StataCorp. Tech Support about it.

Anyway even if you don't take this, maybe a useful starting place for your own commits.

Closes #58

Closes #65 (because it includes those commits)

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@remlapmot remlapmot changed the title Attempt to eplace pkg_resources uses, generally bump to Python 3.10+ maintenance Fixes to DeprecationWarning's and the like Attempt to replace pkg_resources uses, generally bump to Python 3.10+ maintenance etc. Oct 24, 2025
@hugetim
Copy link
Owner

hugetim commented Oct 28, 2025

Thank you! I've been delayed on reviewing but maybe by the end of this week.

@remlapmot
Copy link
Contributor Author

I've been trying to think about the reason for the problem on Python 3.14. My best guess is that it's some problem to do with the filenames being the same, i.e. stata.py in nbstata trying to import something from stata.py in pystata. But so far I don't know how to fix.

Also I wasn't sure if I needed to add corresponding notebooks for the new _resources.py and _version_helper.py files.

@hugetim
Copy link
Owner

hugetim commented Oct 29, 2025

Also I wasn't sure if I needed to add corresponding notebooks for the new _resources.py and _version_helper.py files.

It works to leave them as just .py files, unless you want to add separate tests and docs for them, which is probably not needed.

@remlapmot remlapmot force-pushed the fix-pkg-resources-2 branch from e2ac0d1 to 82010bf Compare November 4, 2025 16:20
@remlapmot
Copy link
Contributor Author

I tried to follow the error when rendering/running in a Jupyer notebook on Python 3.14. I think as well as the stata.py filename issue there might be some problems caused by some of the import statements appearing within nbstata functions/methods. It seems it is more standard Python practice to have all import statements at the top of each .py file and not within function/method definitions.

But even when I've tried renaming the nbstata/stata.py file and moving the import statements out of the function/methods definitions I still end up with errors. [Obvs this testing on a different branch]. So as a short term fix I have removed the Python 3.14 line from the pyproject.yaml file in this PR. But I hope one of us/someone/AI can work this out eventually.

@remlapmot
Copy link
Contributor Author

Hi @hugetim - amazing news - as of today's update to StataNow 19.5 (12 Nov 2025) this now works under Python 3.14. StataCorp have fixed something about pystata or one of their other Python packages bundled within Stata and now the mysterious error I was obtaining above under Python 3.14 is no more.

In help whatsnew it's listed under point 19

CleanShot 2025-11-12 at 21 55 08@2x

So I have,

  • added Python 3.14 back into the pyproject.toml
  • I also bumped the version to 0.8.4

@hugetim
Copy link
Owner

hugetim commented Nov 13, 2025

I wonder whether older versions of Stata will still trigger the issue. And whether they bumped the version number of pystata. (Mostly just writing to acknowledge that I'm still delayed on reviewing, but I haven't forgotten this.)

@remlapmot
Copy link
Contributor Author

Good point - looking at the update pages for the different versions it seems that Stata 18 also received an update yesterday (which in truth I didn't expect). So I am hopeful that we can run under Python 3.14 under both the newly updated Stata 18 and 19 (unfortunately I think I have probably lost my Stata 18 license, so can't check conveniently).

And for users of older versions of Stata, or 18 and 19 before yesterday's update, they can run under Python 3.10, 3.11, 3.12, or 3.13 - which luckily virtual environment managers like uv make it pretty easy to do these days.

Also I should say I don't know if all the elements of the pyproject.toml file are totally correct:

  • I don't know if they are they inline with whatever tool you might have been using to upload to PyPI - maybe you'll need to add extra things for whatever you use
  • For the lower bound of the dependency packages I guess uv has put in what was available when I ran the code - except for the min version of fastcore which I know is the bugfix one we need - so maybe some of those versions can be relaxed or removed
  • Also for the Windows file paths I think there's an edge case in which if a user runs from a Git Bash shell they will need filepaths with forward slashes for their stata_dir - atm they can specify this format of filepath in their stata_dir in their config - but maybe I should add those at the end of the current list?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace pkg_resources uses, generally bump to Python 3.10+

2 participants