Is there a way to not expose credentials in the code? - python

I've been using ipython notebook and there are 2 information (SNOW_USER and PASSWORD) that I need to pass so I can connect to the database. I don't want to expose it for security reasons.
I tried to set as ENV VAR (environmental variables) saving it on my .bash_profile and also on .profile using export SNOW_USER='abc' but it doesn't seem ipython can find it.
import os
print os.environ['SNOW_USER']
I also tried:
%env
But the variables are not showing there either.
Any thoughts on how to do it?

Try to create a file .env somewhere with in it :
export SNOW_USER="snow_user"
export PASSWORD="password"
and then source it :
source .env
Or just source you bash_profile file :
source ~/.bash_profile

Related

modifying the PYTHONPATH variable in zsh

I would like to add my site-packages directory to the PYTHONPATH variable, so I can import modules which are stored there to use them in scripts in interpreter. My default shell is zsh, and the OS is macOS Monterey, version 12.6.
I have tried to open the ~/.zprofile in nano editor from my terminal, adding the path I need, and restarting my terminal, but it did not help to solve the issue. After restarting the terminal, I tried to run following commands:
narynaa#Narynas-MacBook-Pro ~ % echo ~/.z.profile
/Users/narynaa/.z.profile
narynaa#Narynas-MacBook-Pro ~ % cat ~/.zprofile
# Set PATH, MANPATH, etc., for Homebrew.
eval "$(/opt/homebrew/bin/brew shellenv)"
export PYTHONPATH="/opt/homebrew/lib/python3.10/site-packages"
The path I need is stored inside the ~/.zprofile file now, but the problem is not solved.
Thank you for your help!

Alternative Windows/bash command 'workon [insert virtualenv name]' for Mac Terminal [duplicate]

I have installed virtualenv and the virtualwrapper via apt-get, I got to a point where I created a virtual enviroment but however later on during that same day when I used the workon command it was not found. I further on went and inspected my home directory and .virtualenvs dir and the virtualenv I created earlier were still there.
Solving this problem took two steps:
Add this to your .bashrc / .bash_profile / .zshrc:
# load virtualenvwrapper for python (after custom PATHs)
venvwrap="virtualenvwrapper.sh"
/usr/bin/which -s $venvwrap
if [ $? -eq 0 ]; then
venvwrap=`/usr/bin/which $venvwrap`
source $venvwrap
fi
Then use:
source .bash_profile
# or .bashrc / .zshrc
to reflect the changes.
Additionally, if the terminal still sometimes cant find workon, use source .bash_profile to reset and find it again.
type source .profile in home directory from terminal.
Read the readme in the top of which virtualenvwrapper.sh
You need to source it inside bashrc
open ~/.profile
cd ~
nano .profile
add at the end
#virtualenvwrapper setup
export WORKON_HOME=$HOME/envs
export PROJECT_HOME=$HOME/dev
source /usr/local/bin/virtualenvwrapper.sh
to load your .profile file you just edited:
$ . .profile
I ran in to this problem too and I simply needed to logout and log back in.
This read in the changes which the debian package manager made to my system at /etc/bash_completion.d/virtualenvwrapper

Twilio environment variable error

Twilio-Python works fine if I place my account_sid and auth_token directly into the code but will not work when I set them to environment variables. I'm using PyCharm and set them by going to edit configurations > Environment variables, just as I have done with other variables in the past with no problems. I reference them in my code with:
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
Which throws the following error:
twilio.rest.exceptions.TwilioRestException: HTTP 404 error: The requested resource /2010-04-01/Accounts/'<my account_sid>'/Messages.json was not found
I tried exporting the variable on the CLI and running the code there but got the following error:
KeyError: 'twilio_account_sid'
I have no idea what I am overlooking. Any suggestions?
For OS X El Capitan, add environment variables:
open terminal, then
cd ~/
vi .bash_profile
once in vi editor editing the file, get into insert mode (press i) then add these two lines:
export TWILIO_ACCOUNT_SID="AC0123456789abcdefabcdefabcdefabcd"
export TWILIO_AUTH_TOKEN="0123456789abcdefabcdefabcdefabcd"
get into vi command mode (press escape) save and quit
:wq
reboot your computer
To check environment variables after reboot, open terminal, type printenv, you should see them on the list.
In your script, to use the environment variables, try this
import os
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
For configuring Windows environment variables take a look at my answer
Twilio Auth windows enviro variables

Set Path variable in Ubuntu 15.04

I have just moved to Linux (Ubuntu 15.04) and I am trying to add my python directory (:~/Documents/Python/Programs) to the path variable, but I am struggling..
I have tried export PATH = $PATH:~/Documents/Python/Programs, and then turning off and on again, but nothing happens
I have also looked at my ~/.profile, but don't under stand it, it comes up with (I have removed a tonnes of comments from the top):
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
UPDATE:
What I am trying to do is add my python directory to PATH so that I will be able to import self made modules from within this directory
I was under the impression I had to add this to PATH by adding
PATH="$HOME/Documents/Python/Programs/:$PATH"
To the bottom of my ~/.profile document, was this wrong, and what should I actually be doing to solve this?
You have to set your PATH e.g. inside ~/.profile or ~/.bashrc depending on where you want to use Python. Add something like this to the end of either of them:
PATH="$HOME/Documents/Python/Programs/:$PATH"
As stated by the comments, this change will only be take into account, after either restarting a login shell or new start of X session (e.g. newboot). If you need the changes directly, either source the file or export it manually... so
either
source ~/.profile
source ~/.bashrc
(you could also use the . operator, but this is only working in Bash)
or export the variable
export PATH="$HOME/Documents/Python/Programs/:$PATH"
It's important to add your custom path before original PATH as the shell will call the 1st file found inside $PATH.

Create a vim script for django code completion, but fail, why?

I tried two ways to do code completion, one is OK, the other fails.
OK one does like below:
$> cd myDjangoProject/
myDjangoProject $> export PYTHONPATH="."
myDjangoProject $> DJANGO_SETTINGS_MODULE=settings vim urls.py
Then ^x ^o can work well. But this method leads me repeatly do above when edit a file in project.
So an idea comes to me, why not create a script to do above automatically?
Refer to Blog for django code completion in vim, this is exactly what I think, but I encounter a problem during my configuration.
Fail one below:
create a script in /usr/bin named vim_wrapper
#!/bin/bash
export PYTHONPATH="${PYTHONPATH}:/path/to/myDjangoProject/"
DJANGO_SETTINGS_MODULE="/path/to/myDjangoProject/settings" vim $#
Add alias in ~/.bashrc
alias vi="vim_wrapper"
Restart terminal sesstion, command vi /path/to/myDjangoProject/urls.py, make a test :python from django import db, an Error happen says:
ImportError: Could not import settings
'myDjangoProject/settings' (Is it on
sys.path?): Import by filename i s not
supported.
I don't know how to solve this. Thanks for help.
Try just settings DJANGO_SETTINGS_MODULE=settings as you did when it worked. My hope is that your setting of PYTHONPATH will be sufficient.
Create the vim_django executable script in /usr/bin
Content in vim_django script:
#!/bin/bash
PROJECT=`python -c "import os; print os.getcwd().partition('Solutions')[2].split(os.sep)[1]"`
export PYTHONPATH="${PYTHONPATH}:/path/to/django-projects-parent/"
DJANGO_SETTINGS_MODULE=$PROJECT.settings vim $#
Type vim_django urls.py(or other files) to edit in django project, Ctrl-x & Ctrl-o for code completion.
NOTE: In PROJECT settings, you may notice Solutions that is parent directory of all my django projects

Categories

Resources