Learn Python and Django setup

Python Setup

Download later version of python here: https://www.python.org/downloads/windows/ (this link is for Windows OS). Click on download and it will open another page, where you can select which version you want to install based on your operating system. It is usually advised to not install latest version of Python because a lot of libraries support 2 to 3, earlier versions of Python. It is recommended to copy the path where it is going to install the python [PYTHON PATH].

Go to VS code and check latest installed python version by following command in terminal:
Python –version

After that check latest installed pip version. It is used to install Python libraries. You can check it by following command:
pip –version
If it do not give you version information, then it means you need to open ‘System Environment Variable’ by searching in Windows search bar. It open ‘System properties’ tab, where you can click on ‘Advanced’ tab and click on ‘Environment Variables’ button at bottom of page. Then click on ‘path’ and remove any paths related to python. Then add [PYTHON PATH] after clicking ‘New’ button. It will be something like: C:\Users\**YOUR WINDOWS USER NAME**\AppData\Local\Programs\Python\Python313\. Remember to add \ at end of this path and click ‘OK’.

It is recommended to install ‘Python’ Extension in VSCode as well, which is provided by ‘Microsoft’. It can be searched in VSCode ‘Extensions’ menu.

Create a project folder, and open in terminal. Create a simple .py file in it. Click on VSCode ‘Run and Debug’ menu and it will run the file output.

It is advices to create python virtual environment, it keeps separate libraries related to project. Here is the method mentioned to create a python virtual environment: https://code.visualstudio.com/docs/python/environments . In VSCode, there is need to use keyboard shortcut ‘CTRL+SHIFT+P’ to select various options mentioned in above page.

Virtual Environment for project files setup: VENV environment can be activated outside .venv folder in terminal (reference: https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/).
A virtual environment is usually created by following:
python -m venv .venv

and activated by:
.\.venv\Scripts\activate

After that click on [Ctrl+Shift+P], Python[Select Interpreter] and select relevant interpreter with virtual environment name. It will show virtual environment name in bottom right corner of VSCode.

Now let us consider, you want to install NTLK python library in .venv folder, run following code in Python Debug Console:
pip install nltk

Let us consider you want to use NLTK library for language processing, refer code here: https://medium.com/@danielwume/getting-started-with-nltk-10-essential-examples-for-natural-language-processing-in-python-54451eae1366

It is advised that when project on virtual environment, so that relevant libraries can be downloaded and stay with project. So, that in future, if they are not available to download online, project will remain functional offline. These requirements can be listed automatically using following command:
pip freeze > requirements.txt

Some other tricks about Python:

Command to check which Python versions installed on your system:
py -0

If you want to create a virtual environment, using a certain python version (for example: 3.8)
py -3.8 -m venv .venv

After that activate it using:
.\.venv\Scripts\Activate

A lot of different files are dependent on each other and the best method to fix it automatically is using Poetry:
python -m pip install poetry
python -m pip install –upgrade pip
poetry init
poetry add $(cat requirements.txt)
poetry env use “C:\Users\username\AppData\Local\Programs\Python\Python38\python.exe”
poetry add

To clear poetry cache:
poetry cache clear pypi –all

You can do ‘CTRL+SHIFT+P’ and then ‘Python: Select Interpreter’ and select latest virtual environment with relevant python version.

Django Setup

To setup Django, first Python should be installed already, as mentioned in above topic.

1) Create a folder and select that folder in VS code terminal

mkdir MyDjangoProject

cd MyDjangoProject

2) Create a virtual environment (where all project related files will be installed in this folder) for it

virtualenv djangovirtualenv

djangovirtualenv/scripts/activate

Press CTRL+SHIFT+P and select the new virtual environment

3) Install Django

pip install django

django-admin –version

4) Create Django project basic structure

django-admin startproject myproject .

5) To run development server

python manage.py runserver

6) Open a browser and go to http://127.0.0.1:8000/.
You should see the Django welcome page.

7) If you want to install a project ‘scraper’ in django, create it like this:

python manage.py startapp scraper

8) After that there is need to do a lot of changes, like,

In myproject folder, add ‘scraper’, in settings.py file.

In urls.py file of myproject, add following,

from .views import scrape_view
from django.urls import path, include
    path(‘scraper/’, include(‘scraper.urls’)),  # Include scraper app URLs

9) In scraper folder, add files like scrape.py, views.py. And outside scraper folder, add run_scraper.py and add relevant code in these files. Following command automatically includes all dependencies in a file,

pip freeze > requirements.txt

These can be installed quickly using,

pip install -r requirements.txt

10) Project can run using

python run_scraper.py

Leave a Reply

Your email address will not be published. Required fields are marked *