0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu で Python の仮想環境を使う

Last updated at Posted at 2024-12-29

Ubuntu24.10 で pip を使うと次のようなメッセージが出ます。

$ pip install pykakasi
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

指示に従い、仮想環境を使います。

仮想環境を作成

python3 -m venv myenv

仮想環境をアクティベイト

source myenv/bin/activate

切り替わったことを確認

$ which python
/home/uchida/myenv/bin/python

pip でインストール

pip install pandas pykakasi

テスト

python hiragana_sort.py
hiragana_sort.py
import pandas as pd
from pykakasi import kakasi

# ------------------------------------------------------------------
def kanji_to_hiragana(kanji):
	kks = kakasi()
	result = kks.convert(kanji)
	hiragana = ""
	for converted_word in result:
		hiragana += converted_word['hira']
	return hiragana

# ------------------------------------------------------------------
data = {
    'name': ['山田','佐藤','山内','鈴木','伊藤','田中','高橋','野田','相川'],
    'age': [25,30,22,35,28,31,27,21,32]
}

df = pd.DataFrame(data)

df['name_hiragana'] = df['name'].apply(kanji_to_hiragana)

sorted_df = df.sort_values(by='name_hiragana')

print(sorted_df[['name', 'name_hiragana']])

# ------------------------------------------------------------------

仮想環境を非アクティベイトする方法

deactivate
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?