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 thought it will be as simple as adding these locations to Path or PYTHONPATH. I added them to PYTHONPATH and added PYTHONPATH to Path.
When running SET of window's terminal I can see my newly set paths;
E:\Tests> SET
Path=E:\Tests\PythonTests
PYTHONPATH=E:\Tests\PythonTests
(I simplified the list for readability)
I then create a very simple python file test.py inside E:\Tests\PythonTests with a single line:
print ("Hello world")
Now, if I cd \Tests\PythonTests I can run it successfully:
E:\Tests\PythonTests> python test.py
Hello world
If I cd \Tests I can:
E:\Tests> python pythonTests/test.py
Hello world
But if I try
E:\Tests> python test.py
python: can't open file 'test.py': [error 2] No such file or directory
Python version:
E:\Tests\PythonTests>python --version
Python 3.8.0
Am I'm missing something? What am I doing wrong?
The PYTHONPATH env var does not control where the python command searches for arbitrary Python programs. It controls where modules/packages are searched for. Google "pythonpath environment variable" for many explanations what the env var does. This is from python --help:
PYTHONPATH : ':'-separated list of directories prefixed to the
default module search path. The result is sys.path.
Specifying a file from which to read the initial Python script is not subject to any env var manipulation. In other words, running python my_prog.py only looks in the CWD for my_prog.py. It does not look at any other directory.
I need to set some access token environment variables for my python project that I am running in a pipenv. I will want to set these environment variables every time I start the pipenv.
How do I do this?
If you want to load automatically some environment variables each time you start the project, you can set a .env file at the root folder of the project, next to the Pipfile. See Automatic Loading of .env.
You can run the following command from the right folder to create this .env file :
echo MY_TOKEN=SuperToKen >.env # create the file and write into
echo MY_VAR=SuperVar >>.env # append to the file
or just create it manually to obtain:
MY_TOKEN=SuperToKen
MY_VAR=SuperVar
This file will be loaded automatically with pipenv shell or pipenv run your_command and the environment variables will be available.
You can access/check them in your code with :
print(os.getenv('MY_TOKEN', 'Token Not found'))
Not sure about other IDE, but within Pycharm you need the plugin Env File to load it automatically (access Env File tab from the Run/Debug configurations).
You can add comments in this file with a leading #
# My test token
MY_TOKEN=SuperToKen
Note : Of course you must exclude this file from your version control (like git).
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.
I'm trying to get a response from Nagios by using the following Python code and instructions:
http://skipperkongen.dk/2011/12/06/hello-world-plugin-for-nagios-in-python/
From some reason I never get to have OK from Nagios and it's always comes back with the message: Return code 126 is out of bounds - plugin may be missing
I installed nagiosplugin 1.0.0, and still nothing seems to be working
In parallel I have some other services (not python files) that work e.g. http check, current users, and SSH
What am I doing wrong? I'm trying to solve that for few days already
Getting Nagios to utilize your new plug-in is quite easy. You should make changes to three files and restart Nagios — that’s all it takes.
The first file is /etc/nagios/command-plugins.cfg (leave comment please if you know path to this file or analog in ubuntu). Assumed that plugin file is placed in /usr/lib/nagios/plugins/ directory:
command[check_hello_world]=/usr/lib/nagios/plugins/check_helloworld.py -m 'some message'
Drop down one directory to /etc/nagios/objects/commands.cfg (for ubuntu user should create cfg file in that dir /etc/nagios-plugins/config/):
define command {
command_name check_hello_world
command_line $USER1$/check_hello_world.py -m 'some message'
}
Save the file and open up /etc/nagios/objects/localhost.cfg (in ubuntu path to service definition files located in /etc/nagios3/nagios.cfg and by default cfg_dir=/etc/nagios3/conf.d. So, to define new service in ubuntu user should create cfg file in that dir, for example hello.cfg). Locate this section:
#
# SERVICE DEFINITIONS
#
and add new entry:
define service {
use local-service ; Name of service template to use
host_name localhost
service_description Check using the hello world plugin (always returns OK)
check_command check_hello_world
}
All that remains is to restart Nagios and to verify that plug-in is working. Restart Nagios by issuing the following command:
/etc/init.d/nagios restart
http://www.linux-mag.com/id/7706/
ubuntuforums.org - Thread: My Notes for Installing Nagios on Ubuntu Server 12.04 LTS
I had to prepend the path to python2.7 even though the shebang in the file specified it.
In the command definition I had this:
command_line /usr/local/bin/python2.7 $USER1$/check_rabbit_queues.py --host $HOSTADDRESS$ --password $ARG1$
Even though the top of the actual python file had:
#!/usr/bin/env python2.7
Even though the script executed and returned just fine from the command line without specifying the interpreter.
Nothing else I tried seemed to work.