hi i have a question how can run a python script in Batch file
i have a setup file for my flask app
and every time should set up some environment variable and run app.py
i create a setup for this but i don't know how run python app and it's not working
my setup.cmd file looks like this:
set app_debug=True
set API_KEY=secret
set SECRET_KEY=development mode
set WTF_CSRF_SECRET_KEY=development mode
set email_password=development mode
set RECAPTCHA_PUBLIC_KEY=secret
set RECAPTCHA_PRIVATE_KEY=secret
./venv/Scripts/activate.bat
python app.py
and it's goes to ./venv/Scripts/activate.bat and then stop and don't work
I think you have to “call” an external batch file if you want execution to return your current batch file. So write
call ./venv/Scripts/activate.bat
in your batch file.
Consider having a .env file to store your environment variables.
From your app.py file you can load those env vars.
Reference:
https://towardsdatascience.com/environment-variables-python-aecb9bf01b85
Example:
import os
env_var = os.environ.get('ENV')
if not env_var:
print('ENV environment variable does not exist')
I face the same issue with my flask app virtual enviroment setup.bat script, I resolve this issue using below script.
setup.bat
python -m venv venv
call venv\Scripts\activate.bat
pip install -r requirements.txt
call venv\Scripts\deactivate.bat
where, You can see I have used keyword call which will call the activate.bat file and keep my virtual environment activated while I am doing my pip install. Once it is done I have deactivated the virtual environment using the same call command in the batch file.
I'm using a Python virtual environment to load modules that aren't available on our cluster for use in a Hive UDF. I'm unable to source the venv, so when the Python UDF is called in the shell script, the script errors since the modules cannot be found.
When calling ls from the shell script, the venv appears in the list.
DELETE FILE /temp/venv;
ADD FILE /temp/venv;
DELETE FILE udf.sh;
ADD FILE udf.sh;
SOURCE venv/bin/activate;
SELECT TRANSFORM(1)
USING 'bash udf.sh'
AS (test_result)
Results in File: venv/bin/activate is not a file.
SOURCE ../venv/bin/activate;
Results in FAILED: ParseException line 1:2 cannot recognize input near 'This' 'file' 'must'
Within the shell script, If I try to use:
. venv/bin/activate
It returns an exit code 1.
Any thoughts?
Thanks,
Dave
Solved using this: https://stackoverflow.com/a/23069201/10542262
Within the shell script, instead of doing:
python [path]/[script].py
You can call Python from the venv and no longer need to activate the venv.
[path/to/venv/]/bin/python [path]/[script].py
From my Windows 10 command line:
heroku config
yields the output:
=== test-bot Config Vars
GROUPME_BOT_ID: [111111111111111111]
My Python 3 code:
data = {
'bot_id' : os.getenv('GROUPME_BOT_ID'),
'text' : "hi",
}
print(data)
yields:
{'bot_id': None, 'text': 'hi'}
The bot_id is None instead of 111111111111111111. Why?
For testing, I execute my code from the command line of my Windows computer via:
python app.py
heroku:config only sets environment variables on Heroku. To run your code locally you will have to set your environment variables locally. (They can be different in development and production. That's the whole idea—they're environment-specific.)
There are several ways to do this. Here are two:
On your local machine you can use a .env file containing data in the format
VAR1=value
VAR2=value2
and then run your application using Foreman, which will automatically read the .env file and set appropriate environment variables:
foreman start
You can set environment variables using something like direnv, which will read an .envrc file for your project whenever you cd into its directory and set environment variables accordingly. In this case you don't need to use Foreman; python app.py should work fine.
There are editor and IDE plugins for direnv that can automate this in your editor as well.
I have moved my SECRET_KEY value out of my settings file, and it gets set when I load my virtualenv. I can confirm the value is present from python manage.py shell.
When I run the Django Console, SECRET_KEY is missing, as it should. So in preferences, I go to Console>Django Console and load SECRET_KEY and the appropriate value. I go back into the Django Console, and SECRET_KEY is there.
As expected, I cannot yet run a manage.py Task because it has yet to find the SECRET_KEY. So I go into Run>Edit Configurations to add SECRET_KEY into Django server and Django Tests, and into the project server. Restart Pycharm, confirm keys.
When I run a manage.py Task, such as runserver, I still get KeyError: 'SECRET_KEY'.
Where do I put this key?
Because Pycharm is not launching from a terminal, your environment will not be loaded. In short, any GUI program will not inherit the SHELL variables. See this for reasons (assuming a Mac).
However, there are several basic solutions to this problem. As #user3228589 posted, you can set this up as a variable within PyCharm. This has several pros and cons. I personally don't like this approach because it's not a single source. To fix this, I use a small function at the top of my settings.py file which looks up the variable inside a local .env file. I put all of my "private" stuff in there. I also can reference this in my virtualenv.
Here is what it looks like.
-- settings.py
def get_env_variable(var_name, default=False):
"""
Get the environment variable or return exception
:param var_name: Environment Variable to lookup
"""
try:
return os.environ[var_name]
except KeyError:
import StringIO
import ConfigParser
env_file = os.environ.get('PROJECT_ENV_FILE', SITE_ROOT + "/.env")
try:
config = StringIO.StringIO()
config.write("[DATA]\n")
config.write(open(env_file).read())
config.seek(0, os.SEEK_SET)
cp = ConfigParser.ConfigParser()
cp.readfp(config)
value = dict(cp.items('DATA'))[var_name.lower()]
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
elif value.startswith("'") and value.endswith("'"):
value = value[1:-1]
os.environ.setdefault(var_name, value)
return value
except (KeyError, IOError):
if default is not False:
return default
from django.core.exceptions import ImproperlyConfigured
error_msg = "Either set the env variable '{var}' or place it in your " \
"{env_file} file as '{var} = VALUE'"
raise ImproperlyConfigured(error_msg.format(var=var_name, env_file=env_file))
# Make this unique, and don't share it with anybody.
SECRET_KEY = get_env_variable('SECRET_KEY')
Then the env file looks like this:
#!/bin/sh
#
# This should normally be placed in the ${SITE_ROOT}/.env
#
# DEPLOYMENT DO NOT MODIFY THESE..
SECRET_KEY='XXXSECRETKEY'
And finally your virtualenv/bin/postactivate can source this file. You could go further and export the variables as described here if you'd like, but since settings file directly calls the .env, there isn't really a need.
To set your environment variables in PyCharm do the following:
Open the 'File' menu
Click 'Settings'
Click the '+' sign next to 'Console'
Click Python Console
Click the '...' button next to environment variables
Click the '+' to add environment variables
You can set the manage.py task environment variables via:
Preferences| Languages&Frameworks| Django| Manage.py tasks
Setting the env vars via the run/debug/console configuration won't affect the built in pycharm's manage.py task.
Another option that's worked for me:
Open a terminal
Activate the virtualenv of the project which will cause the hooks to run and set the environment variables
Launch PyCharm from this command line.
Pycharm will then have access to the environment variables. Likely because of something having to do with the PyCharm process being a child of the shell.
Same here, for some reason PyCharm cant see exported env vars.
For now i set SECRET_KEY in PyCharm Run/Debug Configurations -> "Environment variables"
In Pycharm manage.py tasks run in an isolated process that do not have access to your environment variables (not the same as Run/Debug).
The simplest solution is to make your python code aware of environment variables by reading them from your .env file directly.
Take a look at: https://github.com/joke2k/django-environ
import environ
env = environ.Env.read_env() # reading .env file
SECRET_KEY = env('SECRET_KEY')
That way you have a single source of configurations, and less settings in the IDE.
Hope that helps,
When using PyCharm along with Django, there are multiple sections where you can set environment variables (EVs):
File > Settings > Languages and Frameworks > Django
There's no purpose to set EVs here
File > Settings > Build, Execution, Deployment > Console > Python Console
There's no purpose to set EVs here
File > Settings > Build, Execution, Deployment > Console > Django Console
Set EVs here and they'll be accesible when using the PyCharm Python Console (which in a Django project opens a Django shell)
File > Settings > Tools > Terminal
Set EVs here and they'll be accesible when using the PyCharm Terminal (i.e when running python manage.py commands in the terminal)
Run > Edit configurations > [Your Django run configuration]
Set EVs here and they'll be accesible when using the PyCharm Run button
Tested on PyCharm 2020.2 using a virtual environment.
To paraphrase #antero-ortiz's answer, instead of using the default Mac double-click or spotlight search to launch PyCharm, you can launch it directly from the terminal. This will inherit all of your environment variables from your terminal session to the PyCharm app.
Here's how I solve this issue.
From your terminal program, run the snippet below.
Validate that things worked by following the instructions to Edit your Run/Debug Configurations and,
Clicking the ... next to Environment variables
Then clicking Show at the bottom right of the screen that pops up. This will show all of the environment variables inherited by the Debug process.
Find your SECRET_KEY in this list. If it's not there, post a comment on this answer and we can try to figure it out!
You'll likely need to launch PyCharm this way every time you start it.
Snippet:
# This file would presumably export SECRET_KEY
source /path/to/env/file
# This is just a shortcut to finding out where you installed PyCharm
# If you used brew cask, it's probably in /opt/homebrew-cask/Caskroom/pycharm
open $(locate PyCharm.app | egrep 'PyCharm.app$')
Side note
Including environment variables in a file that is then source'd is a common pattern for Django development, and the discussion of that best practice is best left out of answers.
based on #rh0dium amazing answer i've made this class:
here's my settings.py:
import os
class EnvironmentSettings():
"""Access to environment variables via system os or .env file for development
"""
def __init__(self, root_folder_path):
self._root_folder_path = root_folder_path
def __getitem__(self, key):
return self._get_env_variable(key)
def __setitem__(self, key, value):
raise InvalidOperationException('Environment Settings are read-only')
def __delitem__(self, key):
raise InvalidOperationException('Environment Settings are read-only')
def _get_env_variable(self, var_name, default=False):
"""
Get the environment variable or return exception
:param var_name: Environment Variable to lookup
"""
try:
return os.environ[var_name]
except KeyError:
from io import StringIO
from configparser import ConfigParser
env_file = os.environ.get('PROJECT_ENV_FILE', self._root_folder_path + "/.env")
try:
config = StringIO()
config.write("[DATA]\n")
config.write(open(env_file).read())
config.seek(0, os.SEEK_SET)
cp = ConfigParser()
cp.readfp(config)
value = dict(cp.items('DATA'))[var_name.lower()]
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
elif value.startswith("'") and value.endswith("'"):
value = value[1:-1]
os.environ.setdefault(var_name, value)
return value
except (KeyError, IOError):
if default is not False:
return default
error_msg = "Either set the env variable '{var}' or place it in your " \
"{env_file} file as '{var} = VALUE'"
raise ConfigurationError(error_msg.format(var=var_name, env_file=env_file))
class ConfigurationError(Exception):
pass
class InvalidOperationException(Exception):
pass
and inside my runserver.py i have this calling code:
from settings import EnvironmentSettings
root_folder_path = os.path.dirname(os.path.abspath(__file__))
env_settings = EnvironmentSettings(root_folder_path)
config_name = env_settings['APP_SETTINGS']
I'm trying this setup currently. I have an env.local file in my project root. My manage.py looks like this:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
# Load environment variables from env.local, if option --load-env specified.
# Good for injecting environment into PyCharm run configurations for example and no need to
# manually load the env values for manage.py commands
if "--load-env" in sys.argv:
with open("env.local") as envfile:
for line in envfile:
if line.strip():
setting = line.strip().split("=", maxsplit=1)
os.environ.setdefault(setting[0], setting[1])
sys.argv.remove("--load-env")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Then I just pass --load-env in the PyCharm run dialog and all the environment variables will be loaded in for the time of the run.
In PyCharm 2018 you can use django manage.py environment variables option in settings
File
Settings
Language and Frameworks
Django
Environment variables
You can see in image
I consider that a good approach is to use django-environ which allow you to read environment variables from a .env file.
Usage
First, install django-environ
pip install django-environ
Second, create a .env file, in the same directory of your settings.py file.
$ cd project-settings-directory/
$ touch .env
Third, add the environment variables you want to use to .env:
# .env file
DEBUG=on
SECRET_KEY=your-secret-key
DATABASE_URL=psql://urser:un-githubbedpassword#127.0.0.1:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
Fourth, in your settings file, add the following just after your import statements:
# other import statements
import environ
# the followin line expects .env file to exist. Otherwise, an IOError exception will be raised.
env = environ.Env.read_env()
Fifth, load the environment variables:
DEBUG = env('DEBUG')
SECRET_KEY = env('SECRET_KEY')
That's it. If you want further information about please review the docs.
Note: this configuration will also help you to set environment variables when running a server from PyCharm.
The answer by #(nu everest) did not work for me. Whatever you described in the question worked for me. You can set all environment variables in the Run/Debug Configurations dialog. This is w.r.t PyCharm 2016.1 and also as per https://www.jetbrains.com/help/pycharm/2016.1/run-debug-configuration-python.html?origin=old_help
Please note that the answer mentioned by "nu everest" works but you will not see Console tab available unless you create a project in pycharm. As individual files can be run without creating a project in pycharm some people might be confused. Although you have also to note that settings are lost to run-configurations that do not belong to a project when pycharm is closed.
Here is my manage.py file in case it helps. It reads all the variables from .env file located in root directory of my project.
In PyCharm 2019+, the debug environment settings are described on their site here. I had similar issues, even with environment variables defined in the other parts of PyCharm (Console, Terminal, etc.)
Summary:
Top-right where your Django project name is (next to the "run" icon), click the drop-down arrow.
i. Note: If the project hasn't been configured for Django, you might not see it. There should be templates available to use in the next window, though.
"Edit Configurations..."
You'll see an "Environment variables" field apart from the console settings. That's the one you actually want to use if you're running the Django server via PyCharm's debugger.