Found 10482 Articles for Python

How to disable logging from imported modules in Python?

Sarika Singh
Updated on 14-Nov-2022 08:02:06

23K+ Views

Applications can use the logging module to configure various log handlers and to route log messages to these handlers. This enables a very flexible design that can handle a wide range of use cases. A caller must first request a named logger in order to produce a log message. The program can set up various rules for various loggers using the name. The program can then utilise this logger to produce plain-text messages at various log levels (DEBUG, INFO, ERROR, etc.), allowing it to handle messages with a higher priority differently from those with a lower priority.Following is an example ... Read More

How do I disable log messages from the Requests Python module?

Rajendra Dharmkar
Updated on 11-Dec-2019 09:57:29

1K+ Views

You can disable logging from the requests module using the logging module.ExampleYou can configure it to not log messages unless they are at least warnings using the following code:import logging logging.getLogger("requests").setLevel(logging.WARNING)If you want to go a level higher and only want to log messages when they are errors or critical, you can do replace logging.WARNING with logging.ERROR and logging.CRITICAL respectively.

What are common practices for modifying Python modules?

Rajendra Dharmkar
Updated on 11-Dec-2019 09:59:06

282 Views

If you are modifying a module and want to test it in the interpreter without having to restart the shell everytime you save that module, you can use the reload(moduleName) function. reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "import moduleName". It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again.For example>>> import mymodule >>> # Edited mymodule and want to reload it in this script >>> reload(mymodule)Note that the moduleName is the actual name of the ... Read More

How we can import Python modules in Jython?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

1K+ Views

You can use pure python modules from jython. You can't use modules that are implemented in C. To use modules from your pip installs, you need to add the sys.path of python to that of Jython as Jython does not automatically pick up the PYTHONPATH informationJython 2.5 introduced the JYTHONPATH environmental variable as Jython-equivalent of PYTHONPATH, so setting both to the same value should do the trick for most use cases (unless you're working in a setup with incompatible Python an Jython versions).Now you can import python modules installed locally directly using 'import' in Jython.

How to Install two python modules with same name?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:43:14

2K+ Views

This is not possible with the pip. All of the packages on PyPI have unique names. Packages often require and depend on each other, and assume the name will not change. Even if you manage to put the code on Python path, when importing a module, python searches the paths in sys.path by order and stops at first match. So whatever module it finds first, it'll stop at that. You best bet is to copy all the code from the libraries to you codebase, change the module name of either and then import it.If you're importing modules with same name from ... Read More

Can we keep Python modules in compiled format?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:44:19

549 Views

Yes you can keep Python modules in compiled format. Python automatically compiles Python source code when you import a module, so the easiest way to create a PYC file is to import it. If you have a module mymodule.py, just do:>>> import mymoduleto create a mymodule.pyc file in the same directory. A drawback is that it doesn’t only compile the module, it also executes it, which may not be what you want. (however, it does compile the entire script even if it fails to execute the script). To do this programmatically, and without executing the code, you can use the ... Read More

How to install python modules and their dependencies easily?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:46:23

2K+ Views

The best and recommended way to install Python modules is to use pip, the Python package manager. It automatically installs dependencies of the module as well.If you have Python 2 >=2.7.9 or Python 3 >=3.4 installed from python.org, you will already have pip and setup tools, but will need to upgrade to the latest version:On Linux or macOS:pip install -U pip setuptoolsOn Windows:python -m pip install -U pip setuptoolsIf you’re using a Python install on Linux that’s managed by the system package manager (e.g "yum", "apt-get" etc…), and you want to use the system package manager to install or upgrade ... Read More

How to use remote python modules?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:47:54

2K+ Views

There are ways to import Python modules remotely. It is not recommended to do so though as it will slow down your app. You can use the knockout module to achieve this. To install knockout use:$ pip install knockoutNow in order to import modules remotely, you can use knockout like:>>> from knockout import urlimport >>> urlimport.register() Url importing enabled. Add urls to sys.path.A valid url looks like this: http://example.com/path/to/repository/#packagenameThis stuff is experimental, use at your own risk. Enjoy.>>> import sys >>> sys.path.insert(0, 'http://www.crummy.com/software/BeautifulSoup/download/3.x/BeautifulSoup-3.0.8/#BeautifulSoup') >>> import BeautifulSoup ... >>> BeautifulSoup If you are not able to install modules on a machine(due ... Read More

How to call a function of a module from a string with the function\'s name in Python?

SaiKrishna Tavva
Updated on 25-Feb-2025 11:23:09

1K+ Views

In Python, you can dynamically call a function from a module by using its name as a string. This is useful in scenarios where function names are not known until runtime. Below are several methods to achieve this. Using getattr() Function Using globals() Function Using locals() Function Using importlib Module Using 'getattr()' Function The getattr() function is a built-in Python function that allows you to retrieve an attribute from an object using a string representing the attribute's name. This is particularly useful ... Read More

How I can install unidecode python module on Linux?

Sumana Challa
Updated on 14-Apr-2025 17:31:41

1K+ Views

What is Unidecode? Unidecode is a Python module that helps convert unidecode text into plain ASCII characters. This is mostly used when the text contains special characters or non-English characters, and you need to simplify it. For example, it converts "kožušček" to "kozuscek", "北京" to "Bei Jing", and "naïve" to "naive". This module is popularly used to simplify for processing or display of user-generated content and international data [multi-lingual]. Installing Python Unidecode Module To install unidecode or any other Python module, you need pip installed(Python package manager). If you have Python 2 >=2.7.9 or Python 3 ... Read More

Advertisements