Skip to content

Commit

Permalink
fix ipython error in pythonrc (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
flexagoon committed Jan 27, 2024
1 parent 4284e63 commit 873f8bb
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion programs/python.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"path": "$HOME/.python_history",
"movable": true,
"help": "Export the following environment variables:\n\n```bash\nexport PYTHONSTARTUP=\"/etc/python/pythonrc\"\n```\n\nNow create the file _/etc/python/pythonrc_, and put the following code into it:\n\n```python\nimport os\nimport atexit\nimport readline\nfrom pathlib import Path\n\nif readline.get_current_history_length() == 0:\n\n state_home = os.environ.get(\"XDG_STATE_HOME\")\n if state_home is None:\n state_home = Path.home() / \".local\" / \"state\"\n else:\n state_home = Path(state_home)\n\n history_path = state_home / \"python_history\"\n if history_path.is_dir():\n raise OSError(f\"'{history_path}' cannot be a directory\")\n\n history = str(history_path)\n\n try:\n readline.read_history_file(history)\n except OSError: # Non existent\n pass\n\n def write_history():\n try:\n readline.write_history_file(history)\n except OSError:\n pass\n\n atexit.register(write_history)\n```\n\n_Note: This won't work if python is invoked with -i flag._\n\n_Credit:_ https://unix.stackexchange.com/questions/630642/change-location-of-python-history"
"help": "Export the following environment variables:\n\n```bash\nexport PYTHONSTARTUP=\"$HOME\"/python/pythonrc\n```\n\nNow create the file _~/python/pythonrc_, and put the following code into it:\n\n```python\ndef is_vanilla() -> bool:\n import sys\n return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0]\n\n\ndef setup_history():\n import os\n import atexit\n import readline\n from pathlib import Path\n\n if state_home := os.environ.get('XDG_STATE_HOME'):\n state_home = Path(state_home)\n else:\n state_home = Path.home() / '.local' / 'state'\n\n history: Path = state_home / 'python_history'\n\n readline.read_history_file(str(history))\n atexit.register(readline.write_history_file, str(history))\n\n\nif is_vanilla():\n setup_history()\n```\n\nFinally, create an empty file at _$XDG_STATE_HOME/python_history_\n\n_Note: This won't work if python is invoked with -i flag._\n\n_Credit:_ https://github.com/b3nj5m1n/xdg-ninja/issues/289#issuecomment-1666024202"
}
],
"name": "python"
Expand Down

0 comments on commit 873f8bb

Please sign in to comment.