{"id":15063645,"url":"https://github.com/cocoa-xu/pythonx","last_synced_at":"2025-03-17T16:11:24.655Z","repository":{"id":246204841,"uuid":"820405367","full_name":"cocoa-xu/pythonx","owner":"cocoa-xu","description":"Python Interpreter in Elixir","archived":false,"fork":false,"pushed_at":"2025-02-10T16:16:07.000Z","size":275,"stargazers_count":65,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-10T08:13:32.300Z","etag":null,"topics":["elixir","elixir-library","python3"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cocoa-xu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-26T12:02:59.000Z","updated_at":"2025-03-09T13:20:25.000Z","dependencies_parsed_at":"2024-06-26T15:06:12.209Z","dependency_job_id":"5be85eca-9988-4f5b-9058-1e6870ab2edc","html_url":"https://github.com/cocoa-xu/pythonx","commit_stats":{"total_commits":134,"total_committers":2,"mean_commits":67.0,"dds":0.007462686567164201,"last_synced_commit":"24ecf6e89e95ce4481d864b5ea0617f90a3be7f3"},"previous_names":["cocoa-xu/pythonx","elixir-pythonx/pythonx"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoa-xu%2Fpythonx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoa-xu%2Fpythonx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoa-xu%2Fpythonx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoa-xu%2Fpythonx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cocoa-xu","download_url":"https://codeload.github.com/cocoa-xu/pythonx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244066180,"owners_count":20392406,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","elixir-library","python3"],"created_at":"2024-09-25T00:05:18.554Z","updated_at":"2025-03-17T16:11:24.617Z","avatar_url":"https://github.com/cocoa-xu.png","language":"Elixir","readme":"# Pythonx\n\n[![Hex.pm](https://img.shields.io/hexpm/v/pythonx.svg?style=flat\u0026color=blue)](https://hex.pm/packages/pythonx)\n\nPython Interpreter in Elixir\n\n\u003e [!IMPORTANT]\n\u003e Pythonx is still very much a work in progress and is mainly intended for some proof of concept at current stage. \n\n## Proof of Concept\n### Low-Level\n\n```elixir\niex\u003e alias Pythonx.C\niex\u003e alias Pythonx.C.PyDict\niex\u003e alias Pythonx.C.PyLong\niex\u003e alias Pythonx.C.PyRun\niex\u003e alias Pythonx.C.PyUnicode\niex\u003e Pythonx.initialize_once()\niex\u003e globals = PyDict.new()\n#Reference\u003c0.1798353731.1418330114.239105\u003e\niex\u003e locals = PyDict.new()\n#Reference\u003c0.1798353731.1418330114.239123\u003e\niex\u003e a = PyLong.from_long(1)\n#Reference\u003c0.1798353731.1418330114.239141\u003e\niex\u003e b = PyLong.from_long(2)\n#Reference\u003c0.1798353731.1418330114.239155\u003e\niex\u003e PyDict.set_item_string(locals, \"a\", a)\ntrue\niex\u003e PyDict.set_item_string(locals, \"b\", b)\ntrue\niex\u003e PyRun.string(\"c = a + b\", C.py_file_input(), globals, locals)\n#Reference\u003c0.1798353731.1418330117.241204\u003e\niex\u003e c = PyUnicode.from_string(\"c\")\n#Reference\u003c0.1798353731.1418330117.241222\u003e\niex\u003e val_c = PyDict.get_item_with_error(locals, c)\n#Reference\u003c0.1798353731.1418330117.241236\u003e\niex\u003e PyLong.as_long(val_c)\n3\n```\n\n### BEAM Level\n```elixir\niex\u003e Pythonx.initialize_once()\niex\u003e globals = Pythonx.Beam.encode(%{})\n#PyObject\u003c\n  type: \"dict\",\n  repr: \"{}\"\n\u003e\niex\u003e locals = Pythonx.Beam.encode([a: 1, b: 2])\n#PyObject\u003c\n  type: \"dict\",\n  repr: \"{'a': 1, 'b': 2}\"\n\u003e\niex\u003e Pythonx.Beam.PyRun.string(\"c = a + b\", Pythonx.Beam.py_file_input(), globals, locals)\n#PyObject\u003c\n  type: \"NoneType\",\n  repr: \"None\"\n\u003e\niex\u003e locals\n#PyObject\u003c\n  type: \"dict\",\n  repr: \"{'a': 1, 'b': 2, 'c': 3}\"\n\u003e\n```\n\n### High-Level\n```elixir\niex\u003e Pythonx.initialize_once()\niex\u003e state = Pythonx.State.new(locals: [a: 1, b: 2])\n%Pythonx.State{globals: %{}, locals: [a: 1, b: 2]}\niex\u003e {result, state} = Pythonx.PyRun.string(\"c = a + b\", Pythonx.py_file_input(), state)\niex\u003e state.locals\n%{\"a\" =\u003e 1, \"b\" =\u003e 2, \"c\" =\u003e 3}\n```\n\n### Very High Level\n#### Python Code Evaluation\n```elixir\ndefmodule MyModule do\n  import Pythonx\n\n  def do_stuff_in_python do\n    a = 1\n    b = 2\n    pyinline(\"c = a + b\",\n      return: [:c]\n    )\n    dbg(c)\n  end\nend\n```\n\nOr in IEx\n\n```elixir\niex\u003e Pythonx.initialize_once()\niex\u003e import Pythonx\niex\u003e a = 1\n1\niex\u003e b = 2\n2\niex\u003e pyinline(\"c = a + b\", return: [:c])\niex\u003e c\n3\n```\n\n#### Executes a command with the embedded python3 executable\n\n```elixir\niex\u003e import Pythonx\niex\u003e python3! \"path/to/script.py\"\niex\u003e python3! [\"path/to/script.py\", \"arg1\", \"arg2\"]\n```\n\n#### Executes a command with the embedded pip module\n\n```elixir\niex\u003e import Pythonx\niex\u003e pip! [\"install\", \"-U\", \"numpy\"]\niex\u003e pip! [\"install\", \"-U\", \"yt-dlp\"]\n```\n\n#### Use an external python library\n\n```elixir\niex\u003e import Pythonx\niex\u003e video_id = \"dQw4w9WgXcQ\"\niex\u003e pyeval(\n...\u003e   \"\"\"\n...\u003e   from yt_dlp import YoutubeDL\n...\u003e   import json\n...\u003e   with YoutubeDL(params={'quiet': True}) as ytb_dl:\n...\u003e     info = ytb_dl.extract_info('https://www.youtube.com/watch?v=#{video_id}', download=False)\n...\u003e     info = json.dumps(info, indent=2)\n...\u003e   \"\"\",\n...\u003e   return: [:info]\n...\u003e )\n[youtube] Extracting URL: https://www.youtube.com/watch?v=dQw4w9WgXcQ\n[youtube] dQw4w9WgXcQ: Downloading webpage\n[youtube] dQw4w9WgXcQ: Downloading ios player API JSON\n[youtube] dQw4w9WgXcQ: Downloading player a95aa57a\n[youtube] dQw4w9WgXcQ: Downloading m3u8 information\niex\u003e IO.puts(info)\n```\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `pythonx` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:pythonx, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\nDocumentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)\nand published on [HexDocs](https://hexdocs.pm). Once published, the docs can\nbe found at \u003chttps://hexdocs.pm/pythonx\u003e.\n\n","funding_links":[],"categories":["Tooling and Utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocoa-xu%2Fpythonx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcocoa-xu%2Fpythonx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocoa-xu%2Fpythonx/lists"}