Remove Python UserWarning

Question:

I just finished installing my MySQLdb package for Python 2.6, and now when I import it using import MySQLdb, a user warning appear will appear

/usr/lib/python2.6/site-packages/setuptools-0.8-py2.6.egg/pkg_resources.py:1054:
UserWarning: /home/sgpromot/.python-eggs is writable by group/others and vulnerable 
to attack when used with get_resource_filename. Consider a more secure location 
(set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
  warnings.warn(msg, UserWarning)

Is there a way how to get rid of this?

Asked By: kagat-kagat

||

Answers:

You can suppress warnings using the -W ignore:

python -W ignore yourscript.py

If you want to supress warnings in your script (quote from docs):

If you are using code that you know will raise a warning, such as a deprecated function, but do not want to see the warning, then it is possible to suppress the warning using the catch_warnings context manager:

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()

While within the context manager all warnings will simply be ignored. This allows you to use known-deprecated code without having to see the warning while not suppressing the warning for other code that might not be aware of its use of deprecated code. Note: this can only be guaranteed in a single-threaded application. If two or more threads use the catch_warnings context manager at the same time, the behavior is undefined.

If you just want to flat out ignore warnings, you can use filterwarnings:

import warnings
warnings.filterwarnings("ignore")
Answered By: jh314

You can change ~/.python-eggs to not be writeable by group/everyone. I think this works:

chmod g-wx,o-wx ~/.python-eggs
Answered By: Waleed Khan
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.