Found 9316 Articles for Python

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

Rajendra Dharmkar
Updated on 30-Sep-2019 08:54:02
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

What are the packages in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:55:02
To understand packages, you also need to know about modules. Any Python file is a module, its name being the file's base name/module's __name__ property without the .py extension. A package is a collection of Python modules, i.e., a package is a directory of Python modules containing an additional __init__.py file. The __init__.py distinguishes a package from a directory that just happens to contain a bunch of Python scripts. Packages can be nested to any depth, provided that the corresponding directories contain their own __init__.py file.When you import a module or a package, the corresponding object created by Python is ... Read More

What does reload() function do in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:55:31
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

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

Rajendra Dharmkar
Updated on 30-Sep-2019 08:56:48
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

Explain python namespace and scope of a variable.

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20
Namespace is a way to implement scope. In Python, each package, module, class, function and method function owns a "namespace" in which variable names are resolved. When a function,  module or package is evaluated (that is, starts execution), a namespace is created. Think of it as an "evaluation context". When a function, etc., finishes execution, the namespace is dropped. The variables are dropped. Plus there's a global namespace that's used if the name isn't in the local namespace.Each variable name is checked in the local namespace (the body of the function, the module, etc.), and then checked in the global ... Read More

What is a namespace in Python?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20
Namespace is a way to implement scope. In Python, each package, module, class, function and method function owns a "namespace" in which variable names are resolved. When a function,  module or package is evaluated (that is, starts execution), a namespace is created. Think of it as an "evaluation context". When a function, etc., finishes execution, the namespace is dropped. The variables are dropped. Plus there's a global namespace that's used if the name isn't in the local namespace.Each variable name is checked in the local namespace (the body of the function, the module, etc.), and then checked in the global ... Read More

How to set your python path on Windows?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:44:06
To set the PYTHONPATH on windows to point Python to look in other directories for module and package imports, go to:My Computer > Properties > Advanced System Settings > Environment VariablesThen under system variables edit the PythonPath variable. At the end of the current PYTHONPATH, add a semicolon and then the directory you want to add to this path:C:\Python27;C:\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 ... Read More

How to set your python path on Mac?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:44:46
To set the PYTHONPATH on Mac OS to point Python to look in other directories for module and package imports, export the PYTHONPATH variable as follows:$ export PYTHONPATH=${PYTHONPATH}:${HOME}/foo In 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

How to set your python path on Linux?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:45:28
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.

How to set python environment variable PYTHONPATH on Windows?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:46:05
To set the PYTHONPATH on windows to point Python to look in other directories for module and package imports, go to:My Computer > Properties > Advanced System Settings > Environment VariablesThen under system variables edit the PythonPath variable. At the end of the current PYTHONPATH, add a semicolon and then the directory you want to add to this path:C:\Python27;C:\foo, In 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 ... Read More
Advertisements