Load environment variables in python - flask

Trying to load environment varibles in python module
import os
class Config(object):
port = os.environ.get("PORT") or 5000
print(Config.port)
$ 5000

This is an easy fix for the problem I just experiences with flask
Create virtual environment using pip or pipenv i.e virtualenv env
Activate the virtual environment source env/bin/acivate
Install python-dotenv module pip install python-dotenv
On top of your python file have something like this
In my .env file I have a variable named MY_VARIABLE
MY_VARIABLE=Somevalue
In my app.py I have this
import os
from dotenv import load_dotenv
# Loading up the values
load_dotenv()
class Config(object):
port = os.environ.get("MY_VARIABLE") or 5000
print(Config.port)
Output
$ Somevalue

Related

"Import flask from Flask" stops working after deactivating env

already searched database within stockoverflow -- there are no answers to this question!
"Import flask from Flask" stops working after deactivating env
installed python3:
"python3 --version" -->returns Python 3.7.4
installed virtualenv
"pip3 install virtualenv"
"virtualenv env"
"virtualenv --version" --> returns 16.7.4
activated virtual environment
"source env/b/activate" --> creates (env) note at terminal prompt correctly
installed flask in virtual environment
" pip3 install flask"
In the python shell, import flask does not return an error.
python program is:
...
from flask import Flask
app = Flask(__ name __)
#app.route('/')
def hello_world():
- return 'Hello, World!'
...
I get an error!
7. the error is: "unable to import flask"
ANY IDEAS ARE APPRECIATED. THANK YOU
I am using the following to run program:
export FLASK_APP=flask_blog.py
pip show flask --> returns: flask not found
pip3 show flask --> returns: flask version 1.1.1
pip doesn't find flask but pip3 does. What does that mean?
virtualenv venv creates a virtual environment that uses Python 2. Since it isn't using Python 3, pip3 install flask uses the system pip3, which lives outside the virtual environment. This will install Flask outside of the virtual environment.
It's unclear how you're invoking Python. If you're typing python3, if you're getting the system python3, which will be able to import Flask.
Regardless, to correct this, remove venv, and build it again using
virtualenv --python=python3 venv
and then either activate the virtual environment, or use one of the wrappers that that virtual environment provides for you. e.g.,
venv/bin/pip install Flask
then
FLASK_APP=app.py venv/bin/flask run

ModuleNotFoundError: No module named 'django' while running manage.py

I have installed virtualenv and then installed django in my Windows 10. After activating virtualenv and running: python manage.py runserver, I am getting:
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
ImportError: Couldn't import Django. Are you sure it's installed and available
on your PYTHONPATH environment variable? Did you forget to activate a virtual
environment?
Also just found while running django-admin.exe I am getting:
Note that only Django core commands are listed as settings are not properly
configured (error: Requested setting INSTALLED_APPS, but settings are not
configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing
settings.).
Manage.py:
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wordcount.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
You can verify whether you have installed django by creating a python program and importing django
import django
print (django.VERSION)
Above code will print the django version installed , confirm whether you have installed django
You have 2 Python versions : The main one which is installed by default and the one used by virtualenv.
When you run pip install django Django is installed in the main version of Python and thats because the PYTHONPATH environment variable refers to path of the main version and not virtualenv.
The same thing happens when you run python manage.py runserver. It doesn't run python from the virtualenv.
To solve this you need to access pip from the virtualenv and then you can insall Django with it
C:\the\path\to\virtualenv\path\to\pip.exe install django
And just like pip, run python.exe from virtualenv
C:\the\path\to\virtualenv\path\to\python.exe manage.py runserver
If you are using PyCharm for development then you can easily set your venv as interpreter.
Now to run django and just like pip you will access python from virtualenv
1 - File > Settings > Your Project > Python Interpreter
2 - Click the settings icon at the right and then click on Add
3 - Click on Virtualenv Environment and choose the location
Once the virtualenv has been setup you can easily use PyCharm to manage packages

How to import global and virtual environment packages in Rodeo?

I have recently installed Tensorflow in a virtual environment and its working fine. I previously installed numpy,pandas,matplotlib and other packages as global packages(python 2.7). I want to use Tensorflow with other global packages using rodeo IDE.
I have successfully imported all packages this way using terminal
virtualenv env --system-site-packages
source /media/usr/dir/tensor/bin/activate
python -c "import tensorflow;print(tensorflow.__version__)"
0.10.0
python -c "import numpy;print(numpy.__version__)"
1.11.1
In rodeo I tried this way ten.sh
#!/bin/bash
virtualenv env --system-site-packages
source /media/usr/dir/tensor/bin/activate
Inside rodeo IDE
#rodeo
import os
os.system('/bin/bash --rcfile /media/usr/dir/tensor/ten.sh')
0
but when tried to import tensorflow its giving me error
ImportErrorTraceback (most recent call last) in ()
----> 1 import tensorflow
I also tried to change the path of the python (default to /media/usr/dir/tensor/bin/python) from IDE itself but after changing when I import any pacakges it return
>>> import tensorflow
This socket has been ended by the other party

No Module Named neo4j in Travis-CI integrated with Python Django

I have built a small python application in Django framework, with Neo4j database hosted on Graphene db. I am integrating Travis-CI for continuous integration with the application on github, however, I am stuck on getting an error in Travis like: ImportError: No module named 'neo4j'
Below is my .travis.yml file:
language: python
python:
- "3.4"
- "2.7"
# command to install dependencies
install:
- pip install -q Django==$DJANGO_VERSION
- pip install py2neo
- pip install neo4django
- pip install -r requirements.txt
# command to run tests
script: python eb_django_app/neo4j/manage.py test
env:
- DJANGO_VERSION=1.8.3
branches:
only:
- master
manage.py :
import os
import sys
from py2neo import neo4j
from py2neo import ServiceRoot
graphenedb_url = os.environ.get("graphene db url", "http://localhost:7474/")
graph = ServiceRoot(graphenedb_url).graph
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "neo4j.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
The folder structure of python application is:
eb_django_app
|_ .travis.yml
|_ requirements.txt
|_ eb_django_app
|_python codebase
|_manage.py
|_neo4j
|_manage.py
|_tests.py
I am new to both Travis and Python. Am i missing something? Can someone please help me out with a solution to resolve this error, would really appreciate some immediate response?

how should i add python path in wsgi.py when using virtualenv

I am trying to deploy a django website using apache2.4, mod_wsgi on ubuntu 14.04.
The problem is that my wsgi.py file is unable to import django. this obviously means that i have not set python path for the virtualenv. But i am a little confused as to how to add python path of virtuaenv's site-packages.
my wsgi.py is:
import os
import sys
sys.path.append('/home/sp/webapps')
sys.path.append('/home/sp/webapps/ilog_dev')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ilog_dev.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
so the question is how should i add the virtualenv's site-package to python path
Have you tried path/to/virtualenv/activate before doing anything? Or maybe you didn't use virtualenv's python instead of global python.