We can find python installer for our operating system from https://www.python.org. There are two types of python installer.
1. Python 2.x
2. Python 3.x
But, Python 2.7 will retire in 2020. We can find countdown site in https://pythonclock.org/ . So, it will be a good decision to learn Python 3.x, though tons of project already done by Python 2.x. Gradually, most of them upgrading themselves.
Python is already installed in Mac and Linux. To find python, just open terminal and write:
$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
But for Windows we need to install it. So, download Python 3.5.x and install. Just click, click and click. As simple as it is.
Virtual Environments: A Virtual Environment is an isolated working environment of Python which allows us to work on a specific project without worry of affecting other projects.
For example, if we can work on a project which requires Django 1.7 on Python 2.7.x while also maintaining a project which requires Django 2.0 on Python 3.x.
virtualenv: As we will emphasize on Python 3.x, we can simply install virtual environment by-
$ pip3 install virtualenv
Creating a virtual environment:
$ virtualenv djproject
This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named djproject.
Now, to begin using the virtual environment, it needs to be activated:
$ source djproject/bin/activate
We can begin installing any new modules without affecting the system default Python or other virtual environments. To check the python env.
(djproject) $ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
Now we can install django or any other module:
(djproject) $ pip install django
(djproject) $ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'2.0.7'
>>>
When we are done working in the virtual environment for the moment, you can deactivate it:
(djproject) $ deactivate
This puts you back to the system’s default Python interpreter with all its installed libraries.
To delete a virtual environment, just delete its folder.
Another way:
Other way of creating virtual environment can be:
$ which python
/usr/bin/python
$ which python3
/usr/bin/python3
$ virtualenv -p /usr/bin/python djproject
or,
$ virtualenv -p /usr/bin/python3 djproject
or,
$ virtualenv -p $(which python3) djproject
Some basic commands:
$ python --version
$ pip --version
$ virtualenv --version
$ pip freeze > requirements.txt
$ pip install -r requirements.txt