Python Basics

·

3 min read

How to find the Python Installed Version:

CMD> python --version
CMD> python3 --version

$ python --version
$ python3 --version

$ which python
$ which python3

How to find Python Installed Location:

Suppose if you have installed Python in two different locations in your system, then how will you find which python you are invoking? How will you find the Python Installed Path?

  1. Open Windows Command Prompt / Anaconda Command Prompt / Linux Terminal:

  2. Simply type "python" which will take you to python command prompt.

  3. Then, inside the python prompt, run the below commands:

$ python (or) python3
>>> import sys
>>> print(sys.executable) # This will tell you the Python Installed Location

How to quit from Python prompt:

Simply type “quit()” in the Python Prompt

How to find the list of Installed Libraries and its Version in Python:

CMD> pip3 list
$ pip3 list

How to install a Library in Python:

General Syntax:
$ pip install <library_name>==<version_no>    ◄◄ To install a specific version, use "==" and "version no"
$ pip install <library_name> ◄◄ To install to the latest version, just remove "==" and "version no"

$ pip3.12 install <library_name>==<version_no>    ◄◄ To install a specific version, use "==" and "version no"
$ pip3.12 install <library_name> ◄◄ To install to the latest version, just remove "==" and "version no"

Real Examples:
$ pip3.12 install pandas==2.1.1    ◄◄ To install a specific version, use "==" and "version no"
$ pip3.12 install pandas ◄◄ To install the latest version, just remove "==" and "version no"

$ pip install --upgrade openai    ◄◄ To install or upgrade the library, use this command.
$ pip3 install --upgrade openai    ◄◄ To install or upgrade the library, use this command.

How to Create and Activate a new Python Virtual Environment for your new Projects:

Once you have Python installed, it is a good practice to create a new virtual python environment for your new projects. Say, for example, if you want to work on OpenAI Python Project, then create a new virtual environment for downloading OpenAI related libraries so that it won't impact your base Python installation libraries.

In general, the Virtual environments provide a clean working space for your Python packages to be installed so that you do not have conflicts with other libraries you install for other projects.

To create a virtual environment, Python supplies a built in "venv" module which provides the basic functionality needed for the virtual environment.

Running the command below will create a virtual environment named "openai-env" inside the current folder

$ python -m venv openai-env ◄◄ Here, "venv" is a keyword. "openai-env" is your own name. You can give any name for the virtual environment.
$ python -m venv vector-project ◄◄ You are just creating a virtual environment in the name "vector-project".

Once you’ve created the virtual environment, you need to activate it.

On Windows, run:
CMD> openai-env\Scripts\activate ◄◄ Here "openai-env" is the virtual environment name which we created in above step. 

On Unix or MacOS, run:
$ source openai-env/bin/activate ◄◄ Here "openai-env" is the virtual environment name which we created in above step.

You should see the terminal / command line interface change slightly after you active the virtual environment, it should now show "openai-env" to the left of the cursor input section.

Python IDEs:

colab.research.google.com
Jupyter Notebook
Visual Studio Code
IDLE
PyCharm Professional

To Install Python:

Anaconda --> This will install Python, Jupyter and Other things.
https://www.python.org/

To get help in Python:

help(package_name.function_name)
or package_name.function_name<question_mark> example: random.randint?