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

440 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

739 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.

How to set your python path on Mac?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:44:46

1K+ Views

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 Windows?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:44:06

755 Views

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

What is zfill() method in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:35:26

95 Views

The zfill method is built for left padding zeros in a string. For example:>>> '25'.zfill(6) '000025'We can also use the rjust(width[, fillchar]) method in string class that right justifies the string and pads the left side with given filler char. The default filler char is space but we can provide it '0' as well. You can use it as follows:>>> '25'.rjust(6, '0') '000025'We can also use Python string formatting to achieve this result as follows:>>> print "%06d" % 25 '000025'

How to import a single function from a Python module?

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

9K+ Views

You can use the "from module import function" statement 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

Get the count of two table fields in a single MySQL query?

AmitDiwan
Updated on 30-Sep-2019 08:28:18

545 Views

For this, you can use the CASE statement along with SUM(). Here, we will be finding the count of Male and Female records from a column with employee gender values. Let us first create a table −mysql> create table DemoTable (    EmployeeGender ENUM('Male', 'Female') ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Male'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Female'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Male'); Query OK, 1 row affected (0.10 sec) mysql> insert into ... Read More

Advertisements