pythonのバージョン確認自分の場合
タグ: if文
-
投稿者投稿
-
2019年8月14日 4:56 PM #614
管理人
キーマスターlinuxですけど端末(ターミナル)で$ python –version だと古い 2 が出てくる。3 入れたのにな ということで python3 –version ってやったら Python 3.6.8 が出て いや3.7 入れたんだけどな ってことで $ python3.7 –version で ようやく Python 3.7.3 が入ってるよと表示されました。 linux mint ですけど
2019年8月18日 4:16 PM #616管理人
キーマスター仮想環境の作成 python3 -m venv 3以降はvenv入ってると書いてるの見たけどyou need to install the python3-venv package using the following command.で apt-get install python3-venv て出たよ。そんじゃということで sudo apt-get install python3-venv インストールはpython3-venv python3.6-venv だって再チャレンジ python3 -m venv kasou んん 出来てるのか出来てないのかわかっらない pwd で現在地 home/次ディレクトリだったので cd kasou ~/kasou$ おお出来てる、はいってますね(これが入ってることになってない 有効になってないのでした)なんか変なので一旦exit 端末を再起動して仮想環境を有効にする source kasou/bin/activate
(kasou) 自分のパソコン名:~$ が表示された これが仮想環境を有効にしてそこに入れたということらしいす ではこの仮想環境でpythonはどれを使ってるのってことで python –version したら Python 3.6.8 だと では場所はディレクトリですけど which python したら /home/自分/kasou/bin/python だと kasouの中にbinができててその中にpythonがあるんだと つまり仮想環境というフォルダが出来てるって感じ ふむふむということで仮想環境から出るというか無効化というらしいコマンド deactivate であたまの(kasou)がなくなったす2019年9月7日 4:58 PM #637管理人
キーマスター仮想環境はできた。webページというかwebアプリケーションというんですかね。これを見るのはサーバーに上げればいいんだろうけど。テスト環境っているよね。ということで調べたら、pythonインストールした段階でWEBサーバもpc内に用意されてるらしい。さっそく書いてあるとおりに端末起動して。うん pythonのバージョンによってなんか違う。じゃ、一旦仮想環境に入って3.6を使ってる環境にしてから書いてみた。webサーバの起動 $ python -m http.server 8000 段変えて Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) … 実行 これで自分のpcでwebサーバー起動してるらしいので ブラウザ立ち上げて http://localhost:8000 したら なにやら ディレクトリーのリストが出てきた。なかに.pyのテストファイルがたまたまあったのでクリックですすむとpythonでprint指定したテキストがブラウザに表示された。ということでpc内にwebサーバーできてましたという確認できた
2019年9月8日 1:57 PM #638管理人
キーマスターwebフレームワークとやらを使うらしい。今時は直書きしないんですね。Djangoというのが多く情報がでてるし人気みたいなので入れてみます。仮想環境を作ってからDjango入れた方がpythonのバージョン管理に適してると思います。では以下の感じでした。$ python3 –version
Python 3.6.8 pythonのバージョン確認しますpip install django でDjangoをインストール
なんだなんだ
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ‘/usr/local/lib/python2.7/dist-packages/pytz-2019.2.dist-info’
Consider using the--user
option or check the permissions.WARNING: You pipre using pip version 19.2.2, however version 19.2.3 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.
なんかディレクトリーとパイソンのバージョンがーーみたいなこと書いてるから仮想環境に。
source kasou/bin/activate で仮想環境を有効化する
改めてインストール pip install django こんどはエラーも警告も出ませんでした。
Successfully installed django-2.2.5 pytz-2019.2 sqlparse-0.3.0 だって 確かめよう
$ python
Python 3.6.8
>>> import django
>>> django.get_version()
‘2.2.5’
これでDjangoはインストール出来てるらしい。つうことはpython3を使う時は毎回仮想環境を有効化しなきゃ2019年9月8日 3:34 PM #639管理人
キーマスターインストールが出来たら、次はプロジェクトとアプリケーションを作成
source kasou/bin/activate 仮想環境有効化
~$ cd kasou
プロジェクト(mySite)を作成したいディレクトリに行き、このコマンドを実行
django-admin startproject mySite
lsで見るとmySiteというディレクトリ(フォルダ)ができてる
cd mySite
~/kasou/mySite$
python manage.py startapp myapp
プロジェクト内にmyappというWebアプリのテンプレート
ls
manage.py mySite myapp ということはmySite内にmanage mySite myappがある
データベースを扱う場合はこのコマンドをmySiteディレクトリで実行する必要
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying admin.0003_logentry_add_action_flag_choices… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying auth.0009_alter_user_last_name_max_length… OK
Applying auth.0010_alter_group_name_max_length… OK
Applying auth.0011_update_proxy_permissions… OK
Applying sessions.0001_initial… OK
ここまでで.pyをDB絡みで作る準備ができました。次は.pyですね2019年9月15日 5:10 PM #642管理人
キーマスターDjangoではデータベースを直接扱わなくても良いように、models.pyに記述されているクラスを操作することで、データベースを操作することが出来ます。
models.pyを編集
models.pyはmyapp/models.pyにあります。
importのあとに半角開けてモジュール名をつけることで、そのモジュールを読み込むことができます。
モジュールのクラスを使う場合は「モジュール名.クラス名」としなければなりません。一度なら良いですが、何度も使う時はいちいちモジュール名を入力するのが面倒です。そんなときはimportだけでなくfromも使ってインポートします。
from モジュール名 import クラス名
では
source kasou/bin/activate 仮想環境有効化
from django.db import models
from: can’t read /var/mail/django.db 場所移動しなきゃいけない
/home/ディレクトリ だから cd kasou/mySite/myapp/
~/kasou/mySite/myapp$ 移動した
ファイル編集 vi models.py
pyファイルを開いて
from django.db import models# Create your models here. これを 以下に
class Person(models.Model):
last_name = models.CharField(max_length=20)
first_name = models.CharField(max_length=20):wq で保存して終了してみた
次に mySite/mySite/settings.py を編集する
ディレクトリを移動する ファイルを開く vi で開いても上手く編集できないので idelでやる
sudo apt-get install idle 足りないidelをインスト
$ idle ターミナルから開く 別に Python 3.6.8 Shellを開く マウス操作でsettings.py開く
下記の2行目追加して保存
INSTALLED_APPS = [
‘myapp.apps.MyappConfig’,
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won’t be applied.
Run ‘manage.py makemigrations’ to make new migrations, and then re-run ‘manage.py migrate’ to apply them.
あれ なんかちがうな
makemigrationを実行することで、Djangoにmodelの変更を知らせて、migrationファイルを作成
python manage.py makemigrations myapp こうか
Migrations for ‘myapp’:
myapp/migrations/0001_initial.py
– Create model Person
よしよし そして migrationファイルを反映させるためには、migrateを
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, myapp, sessions
Running migrations:
Applying myapp.0001_initial… OKこれで、データベースにmodels.pyを反映
models.pyからデータベースに追加したものを、確認
python manage.py shell
コンソールが起動 >>> データを追加
>>>from myapp.models import Person
>>> p = Person(last_name=”Python”, first_name=”Taro”)
>>> p.last_name
‘Python’
>>> p.first_name
‘Taro’
>>>
blogに書いてるとおりに出来てるす2019年9月21日 5:11 PM #646管理人
キーマスター次はtemplate(テンプレートファイルによって定義されたそれぞれのページのデザイン)を使う
HTMLを使って表示するページ HTMLにブロックと呼ばれる変数や命令を埋め込んで、より自由度の高いWebページ
source kasou/bin/activate 仮想環境有効化
プロジェクト(mySite)とアプリケーション(myapp) で プロジェクト内にアプリケーションのテンプレート
Djangoにtemplateファイルを置く場所(ディレクトリ)を知らせる
$ idle ターミナルから開く 別に Python 3.6.8 Shellを開く マウス操作でsettings.py開く
TEMPLATESの’DIRS’に、「os.path.join(BASE_DIR, ‘templates’),」という命令を追加
templatesというディレクトリにtemplateファイルを入れていくことを宣言
TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: [
os.path.join(BASE_DIR, ‘templates’),
],
セーブした
BASE_DIRはDjangoに用意されている変数 os.path.join関数は、Pythonで簡単にパスを作ってくれる関数 ’DIRS’にはtemplatesディレクトリのパスが入っている
どこに、どの名前のディレクトリを置くかを指定しました。
templatesディレクトリは、manage.pyと同じ階層に作ります。
cd kasou/mySite/
mkdir templates 自分でつくるのね
ls
db.sqlite3 manage.py mySite myapp templates できてる
でtemplateファイルっていうhtmlみたいなのをいれるか
idle 開いて新規作成して index.html保存でいいかな
template test
This is template test.
これでtemplateファイルを読み込む準備完了
ここまできたらpythonのsqlを使わないでwebフレームワークという言葉とwebページ構造がようやくわかったような気がする。2019年9月21日 6:34 PM #647管理人
キーマスターviews.pyとurls.pyを設定
view(どのページを表示させるかを決定する処理)とURLを紐つけ
myapp/views.pyを編集
source kasou/bin/activate 仮想環境有効化
idle で 開いて保存def index_template(request):
return render(request, ‘index.html’)myapp/urls.pyをつくる
from django.urls import path
from . import viewsurlpatterns = [
path(‘templates/’, views.index_template, name=’index_template’),
]次に、mySite/urls.py こっちはあるんだな
from django.contrib import admin
from django.urls import path, includeurlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘myapp/’, include(‘myapp.urls’)),
]これ2行目のfrom django.urls import path, include のincludeわすれるよ
mySiteディレクトリで
cd kasou/mySite/
python manage.py runserver 実行すると、サーバーが起動Watching for file changes with StatReloader
Performing system checks…System check identified no issues (0 silenced).
September 21, 2019 – 09:28:09
Django version 2.2.5, using settings ‘mySite.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.ブラウザで http://localhost:8000/myapp/templates/
This is template test. おおでたでた2019年10月13日 4:32 PM #669管理人
キーマスターviewとulrsをやったと、でtemplateというhtmlにあたるファイルをいじる
templateファイルにも変数を書く
template test
This is {{app}} test.
変数に値を追加する作業を、views.py
from django.shortcuts import render
from django.http.response import HttpResponsedef index_template(request):
myapp_data = {
‘app’: ‘Django’
}
return render(request, ‘index.html’, myapp_data)
myapp_dataという辞書型に、appというkeyでDjangoという文字列が入っています。
これをrender関数に渡すことで、変数が反映されます。
source kasou/bin/activate 仮想環境を有効にして
cd kasou/mySite/ ディレクトリに移動して
python manage.py runserver サーバーを起動して
http://localhost:8000/myapp/templates/ ブラウザでアクセスしてtemplate(index.html)を表示
This is Django test. でたでたtemplateファイルの中ではコメントを書くことができます。
{# This is practice. #}
templateファイルは配列を扱うこともできます。
では
source kasou/bin/activate 仮想環境有効化
idle 起動
kasou/mySite/templates/index.html 開いて保存
template test
This is {{app}} test.
{% for i in num %}
{{ i }}
{% endfor %}
{% for 変数 in リスト %}から{% endfor %}までがfor文です。views.py
from django.shortcuts import render
from django.http.response import HttpResponsedef index_template(request):
myapp_data = {
‘app’: ‘Django’,
‘num’: range(10),
}
return render(request, ‘index.html’, myapp_data)
保存し
移動 cd kasou/mySite
サーバー起動 python manage.py runserver
ブラウザで http://localhost:8000/mySite/templates/index.htmlじゃだめで
そこにファイル無いのに http://localhost:8000/myapp/templates/ で表示される
古いwebディレクトリ感覚と違うのね webフレームワークというやつ ulrを別ファイルで繋げたりするやつです。
それぞれのファイルはidleで編集してほうがいいのかなという気がしてきた2019年10月27日 4:58 PM #688管理人
キーマスターtemplateでif文を使うことができれば、表示する内容を条件によって変えることが出来るようになります。
では
source kasou/bin/activate 仮想環境有効化
idle 起動
kasou/mySite/templates/index.html 開いて保存
template test
This is {{app}} test.
{% if is_weekday %}
Today is weekday.
{% else %}
Today is holiday.
{% endif %}
で保存
次もidleでmyapp/views.pyを編集
from django.shortcuts import render
from django.http.response import HttpResponsedef index_template(request):
myapp_data = {
‘app’: ‘Django’,
‘is_weekday’: True,
}
return render(request, ‘index.html’, myapp_data)
これも保存
一旦終わらして再度端末立ち上げて
source kasou/bin/activate
ディレクトリ移動 cd/kasou/mySite/じゃなく cd kasou/mySite
でサーバー起動 python manage.py runserver
で localhost:8000/myapp/templates/
でブラウザ以下の表示
This is Django test.Today is weekday.
上手くいきました -
投稿者投稿
- このトピックに返信するにはログインが必要です。
共有:
- クリックして Twitter で共有 (新しいウィンドウで開きます)
- Facebook で共有するにはクリックしてください (新しいウィンドウで開きます)
- クリックして Tumblr で共有 (新しいウィンドウで開きます)
- クリックして LinkedIn で共有 (新しいウィンドウで開きます)
- クリックして Pinterest で共有 (新しいウィンドウで開きます)
- クリックして Pocket でシェア (新しいウィンドウで開きます)
- クリックして Reddit で共有 (新しいウィンドウで開きます)
- クリックして WhatsApp で共有 (新しいウィンドウで開きます)
- クリックして Telegram で共有 (新しいウィンドウで開きます)
- クリックして Skype で共有 (新しいウィンドウで開きます)
共有:
- クリックして Twitter で共有 (新しいウィンドウで開きます)
- Facebook で共有するにはクリックしてください (新しいウィンドウで開きます)
- クリックして Tumblr で共有 (新しいウィンドウで開きます)
- クリックして LinkedIn で共有 (新しいウィンドウで開きます)
- クリックして Pinterest で共有 (新しいウィンドウで開きます)
- クリックして Pocket でシェア (新しいウィンドウで開きます)
- クリックして Reddit で共有 (新しいウィンドウで開きます)
- クリックして WhatsApp で共有 (新しいウィンドウで開きます)
- クリックして Telegram で共有 (新しいウィンドウで開きます)
- クリックして Skype で共有 (新しいウィンドウで開きます)