setting `PYTHONWARNINGS` to disable python warnings seems to do nothing - python

I'm currently running the openstack executable and it generates python deprecation warnings.
After some searching I did find this howto.
The relevant part is here:
Use the PYTHONWARNINGS Environment Variable to Suppress Warnings in Python
We can export a new environment variable in Python 2.7 and up. We can export PYTHONWARNINGS and set it to ignore to suppress the warnings raised in the Python program.
However, doing this:
PYTHONWARNINGS="ignore" openstack image show image name -f value -c id
does nothing, deprecation warnings are still displayed.
I've tried setting PYTHONWARNINGS to various things:
ignore
"ignore"
"all"
"deprecated"
"ignore::DeprecationWarning"
"error::Warning,default::Warning:has_deprecated_syntax"
"error::Warning"
but none of them seem to do anything.
I was able to work around the issue by appending 2>/dev/null to the end but I would like to know why PYTHONWARNINGS doesn't seem to do anything.

PYTHONWARNINGS certainly does suppress python's warnings. Try running:
PYTHONWARNINGS="ignore" python -c "import warnings; warnings.warn('hi')"
But in this case you are not calling python, but openstack, which is apparently not inheriting the same environment. Without looking at the source I can't say why. It may even be explicitly settings the warning level, which will override anything you do before hand.
If you don't want to see errors, sending STDERR to /dev/null is the proper approach.

You possibly need to "export PYTHONWARNINGS" so it is avaialble to anything openstack invokes

Related

Running pytest inside emacs results in ugly output

If I run pytest inside of Emacs, using M-X compile, $TERM gets set to "dumb", and this seems to confuse pytest's attempt to draw a line of =s across the full width of the screen. It gets the width one too high, resulting in hard to read output with extra folded lines.
Running unset COLUMNS; pytest helps a little, but it's still trying to do some fancy overwriting by issuing raw carriage-returns, and messing that up. I've also tried setting various values for $TERM (ansi, glass-tty, etc). I even tried, unset TERM.
Any way to convince pytest to just produce dumb output and not try to do any fancy output formatting?
I'm also open to ideas on how to pistol-whip emacs into setting the environment correctly :-)
I'm running:
GNU Emacs 22.1.1
Python 3.6.2
pytest version 3.4.0
MacOS 10.12.6 (16G1212)
Terminal Version 2.7.3 (388.1.1)
Try pytest.el, or at least check how it invokes py.test. I use it with Spacemacs (it's included in the Python layer), and have never seen any issues with its terminal handling.

GOPATH interpreted differently when calling directly versus Python subprocess

I recently found a fix for Python getpass not working on Windows: Python not working in the command line of git bash
Or at least that was the last thing I remember about changing my python configurations. (This is for Python 3.6.1 on Windows 10)
Now I also use Python to other tasks which simply has subprocess calls to type several commands on terminal:
go build ./folder/
mv ./src/ ./bin/
I get the error: go: GOPATH entry is relative; must be absolute: "/c/Users/OP/work". But I don't get it if I type go build ./src/folder myself.
I have GOPATH set to C:\work in Environment Variables. I have tried with a ;.
Is there a way to reverse the alias python every time? Or what is happening exactly when setting an alias for python to winpty?
I'm thinking that when I call go build directly, it is called by either my user profile or system. And when python's subprocess calls it, it calls the opposite. Therefore, I have two GOPATH variables even though I have only 1 set in environment variable.
Side Note: another recent change on GOPATH was changing it from C:/go because it couldn't be the same as GOROOT. That error popped up randomly for some reason. It worked with that setting for a while and I don't remember changing anything before except adding another import package on top of the many other ones already being used.
Update: with type python I get the result: python is aliased to 'winpty python.exe'. Therefore I tried to undo that with unalias python. The new result I get is: python is hashed (/c/Users/OP/AppData/Local/Programs/Python/Python36/python).
This fixed the go build command within Python's subprocess. However, that alias was a fix for another Python issue with using getpass package.
On top of my unalias python fix, I also discovered something interesting: when I change the environment variables for GOPATH from C:\work; to C:\go, all go commands still spit the error go: GOPATH entry is relative; must be absolute: "". I got this same error (but different path) from updating Windows 10 Fall Creators update. Maybe it is related.
Simply closing MINGW and reopening it fixed the issue. So perhaps it was saying a copy of my environment variables and using that as a reference instead of the actual system properties.
I know this is not a popular question, but someone could benefit from my hours of investigating and debugging.
Under Windows you must use a Windows style GOPATH like eg d:\code and probably you should use cmd shell and nothing else. Unfortunately cygwin paths (and probably others too) do no longer work especially for go get reaching out to git.
Stick to Windows paths and Windows shell.

See stdout when running bash script in PyCharm

I use a bash script to call several python scripts. I installed the bash plugin for PyCharm. I can run the script, but I don't see stdout during runtime, even though I see it after everything finished. How can I make that visible during runtime?
Without all of the required information, my guess would be that this is due to Python buffering its output, which is its default behavior. You can easily disable this by passing python the -u flag or by setting the PYTHONUNBUFFERED environment variable.
This is described in this SO answer.

What is "python -d" for?

I thought it is for activating debug mode which prints all logging.debug() messages. But apparently that doesn't happen. The documentation simply says:
Turn on parser debugging output (for wizards only, depending on compilation options).
See also PYTHONDEBUG.
Which doesn't explain anything in my eyes. Can somebody give a more verbose explanation and verify that there is really no default CPython Argument that activates debug logging?
From Parser/parser.c:
#ifdef Py_DEBUG
extern int Py_DebugFlag;
#define D(x) if (!Py_DebugFlag); else x
#else
#define D(x)
#endif
This D macro is used with printf() to print debugging messages when the debug flag is supplied and the interpreter is compiled with in debug mode. The debugging messages are intended for the developers of Python, people who work on Python itself (not to be confused with Python programmers, who are people who use Python). I've gone through the Python manual page and none of them activate the logging debug mode. However, one can use the -i flag in conjunction with the -c to achieve the same affect:
python -i -c "import logging;logging.basicConfig(level=logging.DEBUG)"
The -d option enables the python parser debugging flags. Unless you're hacking the Python interpreter and changing how it parses Python code, you're unlikely to ever need that option.
The logging infrastructure is a standard library module, not a builtin feature of the interpreter. It doesn't make much sense to have an interpreter flag that changes such a localized feature of a module.
Also, consider how logging level depends on the logging logger and handler you're using. You can set different levels for different loggers and handlers, for different parts of your application. For instance, when you want all DEBUG lines from anyone to appear in console, but INFO and above from a lib should be logged to a common file, and WARNING and ERROR to be logged to specific files for easier monitoring. You can set a global handler for DEBUG that logs to console, and other handlers that log the different levels to separate files.

Is there a commandline flag to set PYTHONHOME?

I'm attempting to run python on a system that doesn't allow me to set environment variables. Is there a commandline flag to python that will set PYTHONHOME? I looked here: http://docs.python.org/release/2.3.5/inst/search-path.html but didn't see anything.
So, hopefully something like this:
python -magical_path_flag /my/python/install test.py
EDIT
Thanks for the responses everyone. I'm embarrassed to say I actually meant PYTHONHOME, not PYTHONPATH. (That's what I deserve for asking a question at 1:30 AM.) I've edited my quesiton.
Here's some more info. I'm trying to get python running on Android. I can run python -V no problem, but if I try and execute a script, I get:
I/ControlActivity(18340): Could not find platform independent libraries <prefix>
I/ControlActivity(18340): Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Unfortunately when using the ProcessBuilder and changing the environment variables on Android, it says that they're not modifiable and throws an exception. I'm able to pass all the command line flags I want, so I was hoping I could set PYTHONHOME that way.
I've tried creating a wrapping shell script which exports PYTHONHOME and then calls python but that didn't work. (Got the same error as before.)
Thanks,
Gabe
You could simply set it in your script -- sys.path is a regular, modifiable list. Something like:
import sys
sys.path.append("/path/to/libraries")
should do the trick
In UNIXy shells, you can set an environment variable just for the duration of one command by prepending the command with the environment variable setting:
$ PYTHONPATH=/my/python/install python test.py
If none of the other answers suit you, you can write a wrapper script that temporarily sets the environment variable then exec's your other script. For example:
python mywrapper.py -magical_path_flag /my/python/install test.py

Categories

Resources