Google Cloud Platform : Tips & Tricks for python

Pip3 setup for python3

If you are on Red Hat/CentOS: (If ssl module is not available in Python3)

# To allow for building python ssl libs
yum install openssl-devel
# Download the source of *any* python version
cd /usr/src
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
tar xf Python-3.6.8.tar.xz
cd Python-3.6.8

# Configure the build w/ your installed libraries
./configure

# Install into /usr/local/bin/python3.6, don't overwrite global python bin
make altinstall

The easiest way to install pip3 (for python3.x packages) on CentOS 7 is:

$ sudo yum install python34-setuptools
$ sudo easy_install-3.4 pip
or
$ sudo dnf install python3-paramiko
$ sudo dnf install python3

Note : you can change the python 3 version which you installed in your machine.

Now it’s time for updating-package-with-pip

1) To upgrade an existing package, use this :

2) To install the latest version of a package :

3) To install a specific version:

4) To install greater than or equal to one version and less than another:

Example :

pip3 install google-cloud-bigquery

pip3 install --upgrade google-cloud-bigquery

Note : If you get any error related with “python setup.py egg_info”, execute below command.

sudo python3 -m pip install — upgrade pip

Access any gcp services by service account

#Import the libraries
from google.api_core.exceptions import ClientError
from google.oauth2 import service_account
from google.cloud import bigquery

key_path = ‘/path/to/key.json’

credentials = service_account.Credentials.from_service_account_file(
key_path,
scopes=[“https://www.googleapis.com/auth/cloud-platform"],
)

#Initialize a BigQuery Client
client = bigqueryClient(credentials=credentials,project=credentials.project_id)

#Query using the authenticated BigQuery client
query_job = client.query(“””
SELECT
CONCAT(
https://stackoverflow.com/questions/',
CAST(id as STRING)) as url,
view_count
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE tags like ‘%google-bigquery%’
ORDER BY view_count DESC
LIMIT 10"””)

results = query_job.result() # Waits for job to complete.

Let’s Rock…

About Me !