What are the differences between json and simplejson Python modules?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:59:36

578 Views

json is simplejson, added to the stdlib. But since json was added in 2.6, simplejson has the advantage of working on more Python versions (2.4+).simplejson is also updated more frequently than Python. Although they are the same, the version included in the stdlib doesn't include the latest optimizations. So if you need (or want) the latest version, it's best to use simplejson itself, if possible.A good practice, is to use one or the other as a fallback. For example,try: import simplejson as json except ImportError: import json

What is the difference between dir(), globals() and locals() functions in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:56:48

476 Views

locals() returns you a dictionary of variables declared in the local scope while globals() returns you a dictionary of variables declared in the global scope. At global scope, both locals() and globals() return the same dictionary to global namespace. To notice the difference between the two functions, you can call them from within a function. For example, def fun():     var = 123     print "Locals: ", locals()     print "Vars: ", vars()     print "Globals: ", globals() fun()This will give the output:Locals:  {'var': 123} Globals:  {'__builtins__': , '__name__': '__main__', 'fun': , '__doc__': None, '__package__': None}vars() ... Read More

What does reload() function do in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:55:31

1K+ Views

The 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 module, not a string containing its name. In Python 3, reload was moved from builtins to imp. So to use reload in Python 3, you'd have to ... Read More

How can I get a list of locally installed Python modules?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:54:02

3K+ Views

There are multiple ways to get a list of locally installed Python modules. Easiest way is using the Python shell, for example, >>> help('modules') Please wait a moment while I gather a list of all available modules... BaseHTTPServer      brain_nose          markupbase          stat Bastion             brain_numpy         marshal             statvfs CGIHTTPServer       brain_pkg_resources math                string Canvas              brain_pytest        matplotlib       ... Read More

How to check version of python modules?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:53:00

5K+ Views

When you install Python, you also get the Python package manager, pip. You can use pip to get the versions of python modules. If you want to list all installed Python modules with their version numbers, use the following command:$ pip freezeYou will get the output:asn1crypto==0.22.0 astroid==1.5.2 attrs==16.3.0 Automat==0.5.0 backports.functools-lru-cache==1.3 cffi==1.10.0 ...To individually find the version number you can grep on this output on *NIX machines. For example:$ pip freeze | grep PyMySQL PyMySQL==0.7.11On windows, you can use findstr instead of grep. For example:PS C:\> pip freeze | findstr PyMySql PyMySQL==0.7.11If you want to know the version of a module ... Read More

How to retrieve Python module path?

Manogna
Updated on 30-Sep-2019 08:51:02

1K+ Views

For a pure python module you can find the location of the source files by looking at the module.__file__. For example, >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py  Many built-in modules, however,are written in C, and therefore module.__file__ points to a .so file (there is no module.__file__ on Windows), and therefore, you can't see the source. Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to know the location of built-in modules.

How do I unload (reload) a Python module?

Manogna
Updated on 30-Sep-2019 08:50:27

434 Views

The function reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "importmoduleName" without exiting the script. 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 mymoduleand want to reload it in this script >>> reload(mymodule)Note that the moduleName is the actual name of the module, not a string containing its name. The python docs state following about reload function: Python modules’ code is recompiled and the module-level code re-executed, defining ... Read More

What is the use of "from...import" Statement in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:49:27

1K+ Views

The "from module import function" statement is used to import a specific function from a Python module. For example, if you want to import the sin function from the math library without importing any other function, you can do it as follows:>>> from math import sin >>> sin(0) 0.0Note that you don't have to prefix sin with "math." as only sin has been imported and not math. Also you can alias imported functions. For example,>>> from math import cos as cosine >>> cosine(0) 1.0

What is the use of "from...import *" Statement in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:48:52

1K+ Views

The "from module import *" statement is used to import all function from a Python module. For example, if you want to import all functions from math module and do not want to prefix "math." while calling them, you can do it as follows:>>> from math import * >>> sin(0) 0.0 >>> cos(0) 1.0Note that for any reasonable large set of code, if you import * you will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it easy ... Read More

How to set your python path on Linux?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:45:28

734 Views

To set the PYTHONPATH on Linux to point Python to look in other directories for the module and package imports, export the PYTHONPATH variable as follows:$ export PYTHONPATH=${PYTHONPATH}:${HOME}/fooIn this case, are adding the foo directory to the PYTHONPATH. Note that we are appending it and not replacing the PYTHONPATH's original value. In most cases, you shouldn't mess with PYTHONPATH. More often than not, you are doing it wrong and it will only bring you trouble.

Advertisements