This article is the 15th day article of Lisp Advent Calendar 2019.
Hy is a Clojure-like Lisp dialect that runs on Python's Virtual Machine. For Lisper (mainly Clojurian) who has never touched Python, I will focus on how to build a Hy environment.
For an introductory document on Hy itself, please refer to the official tutorial or the one written by the predecessor.
It's easy to download and install the installer from python.org. Suddenly, the latest installer that can be downloaded from the top page of python.org is the 32-bit version (as of December 2019). Be careful not to install it unless you are in a 32-bit environment.
It seems common to install Homebrew and then use the brew
command.
$If you check the installable version with brew info python, 3.7.Was 5(2019/12/At 13 points)。
Location of Python 3 (3.7.5) installed by Homebrew
/usr/local/bin/python3
location of site-packages
/usr/local/lib/python3.7/site-packages
## Building a virtual environment
Even if I install a third-party module in the site-packages of Python itself, it works, but
By using different virtual environments for each project, you can avoid polluting the original environment and avoid affecting other projects.
If you create a virtual environment using the ``` venv``` command that comes by default, it will be created by executing the following under the directory you want to create.
$ python -m venv
If the dependencies of the third-party modules described below are messed up while using the virtual environment, just throw them away and recreate them.
To enable / disable the virtual environment, use the ```activate``` and `` `deactivate``` commands.
The location of ```activate``` in venv depends on the Shell and OS, so [venv --- Create Virtual Environment](https://docs.python.org/ja/3/library/venv. See "Commands for enabling the virtual environment" in html).
## Module installation
When you install Python, the Standard Library (https://docs.python.org/ja/3/library/index.html) is installed with you and you can use them from Hy. Since Python is a language with a built-in battery, you can do a lot with the standard library alone.
If you want to use a third-party module other than the standard library, use the package management tool [pip](https://docs.python.org/ja/3/installing/index.html) to install it in your virtual environment. I will.
Basically, all Python modules (almost) work on Hy.
Of course, modules written in Hy will work if they are the same version (although I rarely see them ...).
Look for third-party Python modules at [PyPI](https://pypi.org/).
Occasionally, there are Python modules that aren't registered with PyPI, but I'll show you how to install them later (for those managed in the GitHub repository).
You can also install the scientific calculation library Anaconda and manage modules with `` `conda```.
```pip```I will not install it in this article as it may damage the environment when used in combination with.
```activate``` the virtual environment and install the module with `` `pip install <module name>` ``.
If you want to install all at once, write requirements.txt with the module names separated by line breaks and run `` `pip install -r requirements.txt```. By convention, it is named requirements.txt, but you can name it something else.
#### **`requirements.txt`**
```text
hy
openpyxl
PySide2
You can also specify a specific version.
requirements.txt
hy==0.17.0
openpyxl==3.0.2
PySide2==5.13.2
If the module is only in the GitHub repository
pip install git+https://github.com/hoge/fuga.git
To do. You can also include this in requirements.txt.
If you want to see a list of third-party modules installed in that environment, run `` `pip freeze```.
pip freeze > requirements.If you write it out like txt, it will be easier to recover even if the environment is corrupted.
## Editor / IDE
Few editors / IDEs have Hy plugins yet. As far as I know, there are only three:
* Emacs
* [hy-mode](https://melpa.org/#/hy-mode)
* Vim
* [vim-hy](https://github.com/hylang/vim-hy)
* Visual Studio Code
* [vscode-hy](https://marketplace.visualstudio.com/items?itemName=xuqinghan.vscode-hy)
# How to make a package
If you want to layer your own applications and libraries, creating a package makes it easier to manage.
hy ─┬─ hoge ─┬─ fuga ─┬─ init.hy . │ . └─ piyo │ └─ bar.hy │ ├─ init.hy . └─ foo.hy
When the `` `hy``` directory is added to the environment variable [PYTHONPATH](https://docs.python.org/ja/3/using/cmdline.html#envvar-PYTHONPATH) in the above figure,
By putting \ _ \ _ init \ _ \ _. Hy in each directory under hoge
#### **`example_import.hy`**
```hylang
(import hoge) ;;hoge__init__.hy is imported
(import hoge.foo)
(import hoge.fuga) ;; hoge/fuga__init__.hy is imported
(import hoge.fuga.bar)
Can be written as The structure of this package is similar to Python.
Python Tutorial> 6. Modules> 6.4. Packages
pip
.Let's have a good Hy life.
Recommended Posts