Skip to main content

Posts

Showing posts with the label Python

Code Snippet for Using Impersonation

Spanner  from google.cloud import spanner import google.auth from google.auth import impersonated_credentials credentials ,project = google.auth.default() #get default credentials and project target_credentials = impersonated_credentials.Credentials(source_credentials=credentials,  target_principals = impersonate_sa_to, #give sa to which you want to impersonate  target_scopes=["https://www.googleapis.com/auth/cloud-platform"] ) spanner_client=spanner.Client(credentials=target_credentials) instance = spanner_client.instance(instance_id) database=instance.database(database_id)

Registering Custom Python Environment in Jupyter Notebook

 Setting up of custom python Environment in Notebook  Refer  RStudio  for using virutalenv Below is the process to setup using conda  from a terminal in Jupiter ,create and activate a new conda environment conda create -n <env-name> conda activate <env-name> (Switch to bash to enable conda) in that new activated environment , install package ipykernel conda install ipykernal Register conda environment python -m ipykernal install --name python-env --display-name "Conda Python Env" --user Refresh the launcher to see new kernel which you created in above process
 Q:Make Conda Environments Available in Jupyter Kernels  A :   #In Base install ipykernel package $conda activate pyenv (pyenv) $conda install ipykernel (pyenv) $ipython kernel install --user --name=pyenv_kernel  #pyenv_kernel name that help to link the kernel back to conda environment #To remove a kernel  $conda activate pyenv (pyenv) $jupyter kernelspec uninstall pyenv_kernel #TO list currently available kernels  $jupyter kernelspec list

Python Module Installation Issues and Work Arrounds

Problem :  When Running PYODBC in Mac , User getting Error "pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection (0) (SQLDriverConnect)')"  Solution :  Run the following commands rm -rf /usr/local/opt/openssl version=$(ls /usr/local/Cellar/openssl@1.1 | grep "1.1") ln -s /usr/local/Cellar/openssl@1.1/$version /usr/local/opt/openssl Source: https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/known-issues-in-this-version-of-the-driver?view=sql-server-ver15

Python - Working With Conda

 Python - Working with Conda Getting Conda Environment Info $conda info Login to Conda Environment $export PATH=”/app/miniconda3/bin:$PATH” $source activate [env_name] How to get Module installed path >>>import module >>>print(module.__file__)   Export Modules to file $conda list --explicit > env_name.txt Listing Environments $coda env list Creating New Environment $conda config --add channels conda-forge   #after this we can get file ~/.condarc $vi condarc #set proxy in condarc file ~/.condarc channels: -           conda-forge -           defaults proxy-servers:       http:http://company.com       https:http://company.com   $conda create --name [env_name] --f...