· 

Installing Django on PythonAnywhere (Part 2)

Coursera: Web Application Technologies and Django

[Started on March 2025]

https://www.youtube.com/watch?v=ZsEu-jyy2qY

https://www.pythonanywhere.com/

https://www.dj4e.com/assn/dj4e_install.md

 

➡ Read Part 1.

 

1. What have I done so far?

前回は、login ID を作るところまでやりました。今回はその続きです。

 

2.  https://www.dj4e.com/assn/dj4e_install.md

(1) このサイトのinstructionsにそってinstallしていきます。

 

Once you have created your PythonAnywhere account, start a bash shell and set up a virtual environment with Python 3.x and Django 4.2.

 

まず、Djangoのlatest versionが4.2なのかどうか調べます。

version 5.2が現時点で最新でした。

それから、Pythonのlatest versionも調べておきます。

Python のlatest versionは3.13.2でした。私のシステムのversionは、3.12.3なので、latest versionをinstallしておきます。インストールした後、3.12.3をuninstallし、pathをdeleteし、start menuからもdeleteしました。

C:\Users\[username]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

からpython3.12を探して、削除すると、start menuから消えます。GitBashでPythonのversionを確認します。

3.13.2になりました。

 

ということは、上記のinstructionsは、

 

Once you have created your PythonAnywhere account, start a bash shell and set up a virtual environment with Python 3.13 and Django 5.2.

 

と読み替えることになります。

 

(2) 具体的には、bash consoleで、

mkvirtualenv django42 --python=/usr/bin/python3.9

pip install django==4.2.7 ## this may take a couple of minutes

を実行するとのことですが、これも

mkvirtualenv django52 --python=/usr/bin/python3.13

pip install django==5.2 ## this may take a couple of minutes

に書き換えることにします。

 

しかし、まだ不安なので、先に、このcommandが何をするのか、ChatGPTに聞いておきます。

長くなりそうなので、章を変えます。

 

3. ChatGPTとの会話

 

(1) まずは、ChatGPTへのpromptです。

 

Tell me about the following bash command.

mkvirtualenv django52 --python=/usr/bin/python3.13

pip install django==5.2 ## this may take a couple of minutes

 

Let's break down the Bash commands:

1. mkvirtualenv django52 --python=/usr/bin/python3.13

  • mkvirtualenv: This command creates a new Python virtual environment using virtualenvwrapper.

  • django52: This is the name of the virtual environment being created.

  • --python=/usr/bin/python3.13: This specifies that the virtual environment should use Python 3.13 (which is not officially released yet, as of now).

 

🔹 Potential Issue: Python 3.13 may not exist on your system. You can check available versions with:

ls /usr/bin/python*

If Python 3.13 isn't installed, you'll need to install it before running this command.


2. pip install django==5.2

  • pip install: Installs Python packages.

  • django==5.2: Installs Django version 5.2.

🔹 Potential Issue: Django 5.2 is not released yet. You might need to use a stable version like 5.0 or check for the latest version with:

 

 

pip install django --upgrade

Final Notes:

 

  • Ensure Python 3.13 is installed before using mkvirtualenv.

  • If you don't have virtualenvwrapper, install it first:

pip install virtualenvwrapper

  • If mkvirtualenv isn't working, try:

python3 -m venv django52

source django52/bin/activate

Would you like help installing the latest stable Python version or setting up Django properly? 😊

 

(2) ChatGPTの回答1で、your systemにPython3.13があるか調べるとよいとのことなので、調べてみます。ここでの、your systemとは、localでのsystemではなくて、PythonAnywhereでの私のsystemという意味なので、bash consoleを開いて、commandを入力してみます。

python3.7か3.13まで入っていました。

 

ついでに、このPythonAnywhereでの、filesの構造がどうなっているか調べておきます。まず、root directryを見てみます。

たぶん、全部folderだと思いますが、いちおう区分けしてみます。

これだと、lib32やlibx32はexecutableに見えますが、実際には、別のfolderを指しているだけで、executableではないです。

 

home directoryも見ておきます。

txt fileが1つあるだけです。中身も見ておきます。

home direcrtoryの隠しファイルも見ておきます。

pathも見ておきます。

pathの最初に、

/home/[user]/.local/bin

とあるので、その中身を見てみます。

ここにpythonとかpython3がありますが、よくわからないので、詳しく見て見ます。

画像右側に、python -> /usr/local/bin/python3.13*等があります。これは、shortcutのようなもので、pythonと入力すると、それは、/usr/local/bin/python3.13を指す、というものです。この、~/.local/binというフォルダ自体が、pathの最初に置いてあるので、結局、pythonと入力すれば、それは、/usr/local/bin/python3.13と入力したのと同じことになります。その他、そこにある、python3、pip、pip3等も同様です。

 

(3) ChatGPTとの会話に戻ります。PythonAnywhereの中に、

/usr/bin/python3.13

があるのは確認できました。ということは、PythonAnywhereの中に、mkvirtualenvというcommandとなるfileもどこかにあるはずです。探してみます。

 

見つかりませんでした。ChatGPTに聞いてみました。どうやら、virutalenvwrapper.shというfileの中のcommandとしてのみ存在するようです。

 

The following is a part of .bashrc.  What does it mean?

# Load virtualenvwrapper

source virtualenvwrapper.sh &> /dev/null

 

Breakdown of the Command:

  1. source virtualenvwrapper.sh

    • This runs (or "sources") the virtualenvwrapper.sh script.

    • virtualenvwrapper.sh is a shell script that sets up commands like mkvirtualenv, workon, rmvirtualenv, and lsvirtualenv.

    • It initializes the environment for virtualenvwrapper, ensuring that virtual environments are stored and managed properly.

  2. &> /dev/null

    • This redirects both stdout (standard output) and stderr (error messages) to /dev/null, effectively silencing any messages or errors that the script might produce.

    • If the script runs successfully, nothing will be printed to the terminal.

    • If virtualenvwrapper.sh is missing or misconfigured, you won't see an error message, which can sometimes make debugging harder.

Why Use This in .bashrc?

  • .bashrc is executed whenever you start a new Bash session (like opening a terminal).

  • This line ensures that virtualenvwrapper is available automatically every time you open a new terminal.

  • The &> /dev/null part is used to prevent unnecessary output from appearing during shell startup.

Potential Issue

If virtualenvwrapper.sh is missing or incorrectly installed, this line will silently fail, and mkvirtualenv and other related commands won't work. To check if it's actually being loaded, remove &> /dev/null and restart your shell:

bash
source virtualenvwrapper.sh

If there's an issue, it will show an error message.

 

Would you like help troubleshooting your virtualenvwrapper setup? 😊

 

つまり、bash起動時に、.bashrcが実行され、その中の

source virtualenvwrapper.sh &> /dev/null

により、mkvirutalenv等のcommandsが使えるようになるとのことです。

また、virualenvwrapper.shというfileは、

/usr/local/bin

の中にあり、これは先ほど見たpathの中にありました。

 

ChatGPTの回答2の

pip install ...

については、よく見るcommandなので、そのまま使います。

 

(4) いろいろ調べる途中で見つけた役に立ちそうなサイトを紹介します。

virtualenvwrapperについて詳しい説明がなされています。

bash commandsについて書かれています。

 

 

4. 2つのcommandsの実行

 

(1) さて、以上により、instructionsに書かれた2つのcommands (mkvirtualenv...、pip install ...)は、正常に実行できそうなので、やってみます。まずは、mkvirutalenv...です。

時間がかかると書いてありましたが、30秒ほどで終わりました。画像の一番下のところに、virtualである表示 (django52) がなされています。

 

(2) 次は、pip install...です。このvirtual environment上にinstallすることになります。

これについては、5~10分程度かかった様子です。下の方に、Successfully installed...と出ており、installが成功したようです。

 

(3) installしたversionの確認です。

PythonAnywhereにもともと入っていたのは、python3.13.1なので、この表示で正しいです。また、今回installしたのは、Django5.2なので、それもあっています。

 

(4) なお、djangoのversionを調べるときに、

django --version

だとerrorが出ます。これについて、ChatGPTに聞いてみました。

 

Why do you need python -m in the following command?  Doesn't django --version work?

 

Good question! Both commands serve a similar purpose, but there are key differences:

1. python -m django --version

  • Runs the django module using Python.

  • Ensures that the django package being used is the one associated with the currently active Python environment.

  • If you have multiple versions of Python installed, this guarantees that you're checking Django's version in the correct Python environment.

  • Useful when working in virtual environments.

2. django --version

  • Calls the django-admin command directly if it's available in the system's PATH.

  • May not work if django-admin is not in the PATH, especially in virtual environments.

  • Could potentially reference the wrong Django installation if multiple versions exist on your system.

When Should You Use python -m django --version?

  • When working inside a virtual environment to ensure the correct Django version.

  • When debugging to check which Python interpreter is running Django.

  • When you're unsure whether django-admin is properly configured in your PATH.

 

If you're confident that your shell is correctly set up and using the right virtual environment, then django --version should work fine. 😊

 

 

また、-mは、

The -m flag in Python stands for "module". 

だそうです。

 

5. workon, deactivate

 

(1) workonがどんなcommandなのかChatGPTに聞いてみました。

 

What does the following command do?

workon djago52

 

The command:

bash
workon django52

What It Does:

  • Activates the virtual environment named django52.

  • workon is a command provided by virtualenvwrapper (which must be installed and sourced in your .bashrc or .bash_profile).

  • Once executed, your terminal prompt usually changes to indicate that you're inside the django52 virtual environment.

Prerequisites:

  • You must have virtualenvwrapper installed and set up.

  • The virtual environment django52 must already exist (created using mkvirtualenv django52).

  • If django52 does not exist, you’ll see an error like:

    sql
    ERROR: Environment 'django52' does not exist. Create it with 'mkvirtualenv django52'.

How to Exit the Virtual Environment:

To deactivate it, simply run:

bash
deactivate

 

Would you like help verifying whether django52 exists on your system? 😊

 

すなわち、workonはvirtual environmentに入るためのcommandでした。また、そこから抜け出すには、deactivateを入力すればよいとのことです。

 

(2) 実際にやってみます。今、django52というvirtual environmentにいるので、deactivateしてみます。

 抜け出しました。時刻の前の、(django52)が消えました。では、もう一度、django52に入ってみます。

入りました。(django52)という表示が現れました。

 

6. Automatically Enabling Your Virtual Environment

 

(1) Bash consoleを開いたとたんに、django52に入るには、.bashrcの中に、

workon django52

を入れておけばよいです。入れる位置は、

source virtualenvwrapper.sh &> /dev/null

の次の行に入れておけばよいです。

 

(2) そこで、nanoを使って、.bashrcをeditしてみました。

nanoは、初心者向けで、使いやすいです。

 

How can I edit a file on bash?

 

You can edit a file on Bash using a text editor like nano, vim, or vi. Here are some common ways to edit a file:

1. Using nano (Beginner-Friendly)

bash
nano filename.txt
  • Use arrow keys to navigate.

  • Make changes to the file.

  • Press CTRL + X, then Y, then Enter to save and exit.

2. Using vim or vi (Advanced)

bash
vim filename.txt

 

  • Press i to enter insert mode and edit the file.

  • Press ESC, then type :wq and press Enter to save and exit.

  • If you want to exit without saving, press ESC, then type :q! and press Enter.

 

(3) うまく、.bashrcが動くか試すために、bash console自体からexitしました。

exit

で、現在のbash consoleが終了します。

 

I am using PythonAnywhere and working on a bash console.  I want to exit from the bash console.  Is it possible?

 

Yes, you can exit the Bash console in PythonAnywhere using any of the following commands:

  1. Exit the shell normally

    bash
    exit
    • This will close the Bash console.

  2. Use Ctrl + D

    • Press Ctrl + D to send an EOF (End of File) signal and exit the shell.

  3. Forcefully terminate the session

    bash
    kill -9 $$
    • This forcefully kills the current shell process.

 

Let me know if you need more help! 😊

 

(4) PythonAnywhereのdashboardに戻って、新しいbash consoleを起動しました。

時刻の前に (django52)が表示されて、virtual environmentでの起動が確認できました。

 

また、左上にconsole番号が表示されるのですが、その番号も違うものになっていました。しかし、.bashrcは変更したとおりになっており、また、django5.2もinstallされていることから、consoleは新しいものになりましたが、保存してあるfile等は、前回のconsoleのときのままだと考えられます。

 

前回のconsoleのときのdisplay内容は見れないのか、ChatGPTに聞いてみました。

 

After exiting the bash console, I started a new bash console.  What I created on the previous console was intact.  Can I see the history of the contents displayed on my previous console?

 

No, you cannot see the history of the contents displayed on your previous Bash console session. Each new Bash console in PythonAnywhere starts fresh and does not retain the screen output from the previous session. However, you can still access the command history.

How to View Your Previous Commands:

  1. Use the history command

    bash
    history
    • This shows a list of commands you executed in previous sessions.

  2. Use the up/down arrow keys

    • Press the Up Arrow () to scroll through previously used commands.

  3. Search for a previous command interactively

    • Press Ctrl + R and type part of a command to search for it in history.

If you want to log all previous session outputs, you can use a tool like script:

bash
script session.log

This will record all terminal output into session.log, which you can view later.

 

Would you like help setting up persistent logs for future sessions? 😊

 

答えは、noでした。ただし、

history

というcommandで、使ったcommandsのリストは表示できるとのことでした。

 

7. Where is my django52

(1) 今回のブログでinstallしたdjango5.2が、PythonAnywhereのどこに保存されているか確認してみます。まず、virtual environmentのdjango52というfolderですが、

/home/[user name]/.virtualenvs/django52/

にあります。さらに、この中のbinにあるdjango-adminが、django5.2のinstallしたもののようです。

django-admin --versionと入力すると、version番号の5.2が表示されました。

 

さらに、django-admin --helpと入力すると、各commandの説明が表示されました。

したがって、今回installしたdjango5.2の本体は、

/home/[user name]/.virtualenvs/django52/bin/django-amdin

であると考えられます。

 

(2) なお、

 

/usr/.local/bin

にも、django-adminがありますが、これを詳しく見ると、

 

 /usr/local/bin/django-admin-3.13

を指しています。そこで、このfileのところへ行って、versionを調べてみます。

 

5.1.3であって、5.2ではありませんでした。

 

そこで、もう一度、pathを調べてみます。

pathの一番最初は、

/home/[user name]/.virtualenvs/django52/bin

となっています。そして、その次に、

/home/[user name]/.local/bin

が来ています。これら、2か所のbinの中に、同一名称のdjango-adminが入っており、前者のbinのdjango-adminはversion 5.2であり、後者のbinのdjango-adminは、さらに

/usr/local/bin/django-admin-3.13

を指しており、これは、version 5.1.3でした。しかし、前者の方が、pathでは優先順位が高いので、django-adminは、version 5.2であることになります。

 

(3) 仮想環境の中のPython3.13.1ですが、

/home/[user name]/.virtualenvs/django52/bin/

/home/[user name]/.virtualenvs/django52/lib/

 

の両方に入っています。

 

8. bashのeditorの補足

上記では、.bashrcをeditするのに、bash上のnanoを使いました。

その他に、PythonAnywhereのdashboardからfilesに入り、.bashrcをクリックすると、editorに入れます。そこでeditして保存することも可能でした。

 

9. What's next?

 

ひとまず、djangoのinstallまでは終わったので、今回はここまでとします。

 

次は、Part 3.