Tana Gone
Tana Gone
1 min read

Categories

Rubyではbundleコマンドを使ってプロジェクトを作る。Pythonではvenvモジュールを使えばプロジェクトを作ることが出来る

1. Rubyプロジェクト作り方

$bundle init
$echo "gem 'mysql2'" > Gemfile
$bundle config set path vendor
$bundle install
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Fetching bigdecimal 4.0.1
Installing bigdecimal 4.0.1 with native extensions
Fetching mysql2 0.5.7
Installing mysql2 0.5.7 with native extensions
Bundle complete! 1 Gemfile dependency, 3 gems now installed.
Bundled gems are installed into `./vendor`
$echo "require 'mysql2'" > a.rb
$bundle exec ruby a.rb # no error

errorの例

$ruby a.rb
<internal:/opt/homebrew/Cellar/ruby/3.4.2/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:136:in 'Kernel#require': cannot load such file -- mysql2 (LoadError)
...

2. Pythonプロジェクトの作り方

$mkdir workingTree ; cd workingTree; python -m venv .
$source bin/activate
$python -m pip install numpy
$echo "import numpy as np
x = np.array([1, 2, 3])
print(str(x))
" > a.py
$python a.py # [1 2 3]
$deactivate

3. Python依存パッケージの確認

$pip freeze > requirements.txt

依存ファイルリストを使ってインストール

$python -m pip install -r requirements.txt

PytorchをInstallすればMetal対応のが自動的にInstallされる。約500MBのDiskを消費する。

(test02) [13675@5:13]$python
Python 3.13.2 (main, Feb  4 2025, 14:51:09) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
... # create a 3D tensor from NumPy array
... ary3d = np.array([[[1, 2], [3, 4]],
...                   [[5, 6], [7, 8]]])
... tensor3d_2 = torch.tensor(ary3d)  # Copies NumPy array
>>> print(tensor3d_2)
tensor([[[1, 2],
         [3, 4]],

        [[5, 6],
         [7, 8]]])
>>> print(torch.backends.mps.is_available()) # Metal Performance Shader
... print(torch.__version__)
True
2.9.1
>>> import os
>>> os.system('du -sh')
543M	.

LLMs-from-scratch/appendix-A/01_main-chapter-code/code-part1.ipynb at main · rasbt/LLMs-from-scratch

Scientific Computing in Python: Introduction to NumPy and Matplotlib: Sebastian Raschka, PhD

PyTorch documentation — PyTorch 2.9 documentation

4. uvコマンドを使った仮想環境構築

uvコマンドを使えば、venvモジュールとは異なりactivate/deactivateしなくても作業開始できる。更にgit init済。

$mkdir uv_test
$cd uv_test
$uv init . 
Initialized project `uv-test` at `.../uv_test`
$ls
main.py        pyproject.toml README.md
$uv add numpy
Using CPython 3.10.20
Creating virtual environment at: .venv
Resolved 4 packages in 166ms
Installed 1 package in 41ms
 + numpy==2.2.6
$vi main.py
$uv run python main.py
Hello from uv-test!

5. Pythonコードのdebugはpdbモジュールを呼び出せばdebug対象の書き換え不要

$cat hello.py
print("Hello")

$uv run python -m pdb hello.py # <- ココがポイント
> hello.py(1)<module>()
-> print("Hello")
(Pdb) l
  1  -> print("Hello")
[EOF]
(Pdb) b 1
Breakpoint 1 at hello.py:1
(Pdb)

6. Jupyter Notebook始め方

$uv add ipykernel # might be unnecessary.
$uv add jupyter notebook
$uv run jupyter notebook # to open the Safari.
# if you encounter this
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 2
      1 import numpy as np
----> 2 import pandas as pd

ModuleNotFoundError: No module named 'pandas'
$ uv add pandas