
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10483 Articles for Python

1K+ Views
To check if you can import something in Python 2, you can use imp module with try...except. For example, import imp try: imp.find_module('eggs') found = True except ImportError: found = False print foundThis will give you the output:FalseYou can also use iter_modules from the pkgutil module to iterate over all modules to find if specified module exists. For example, from pkgutil import iter_modules def module_exists(module_name): return module_name in (name for loader, name, ispkg in iter_modules()) print module_exists('scrapy')This will give the output:TrueThis is because this module is installed on my PC.Or if you ... Read More

7K+ Views
The Python Standard Library is a collection of script modules that may be used by a Python program, making it unnecessary to rewrite frequently used commands and streamlining the development process. By "calling/importing" them at the start of a script, they can be used. A module is a file that contains Python code; an ‘coding.py’ file would be a module with the name ‘coding’.We utilise modules to divide complicated programmes into smaller, more manageable pieces. Modules also allow for the reuse of code. In the following example, a module called ‘coding’ contains a function called add() that we developed. The ... Read More

2K+ Views
Python does not have a data type for dates, but we may import the datetime module to work with dates as date objects. Calculating dates that are months apart from a given date is quite challenging due to the varying length of months in our calendar system. This article tells about how to display the current date by importing the datetime module. There are two main methods to calculate a date six months from now in Python, which includes - Using relativedelta() function Using timedelta() function Using relativedelta() Function ... Read More

227 Views
You can use "Sandboxed Python". A "Sandboxed Python" would let you permit or forbid modules, limit execution slices, permit or deny network traffic, constrain filesystem access to a particular directory (floated as "/"), and so on. It is also referred to as RestrictedExecution. There are many ways to implement sandboxing on Python. You could Modify the CPython Runtime, Use Another Runtime, Use Operating System Support, etc to implement such a sandbox. You can read more about sandboxing at: https://wiki.python.org/moin/SandboxedPythonPypi has a package called RestrictedPython(https://pypi.python.org/pypi/RestrictedPython) that is a defined subset of the Python language which allows to provide a program input ... Read More

272 Views
There is no straightforward way to do this. But it is possible to run a Python program and parse the output. You can execute any shell command using the function system (cmd, flag). The second argument is optional. If it is present, the output of the command is returned by system as a string. If it is not supplied, any output from the command is printed, with the standard output filtered through the pager. For example,output = system ("python /path/to/your/python/script.py", 1)

4K+ Views
If you have your own Python modules you want to copy, you can simply copy them and run on other systems with Python installed. If you want to copy installed modules, the best way is to install the same version of Python on the second system. Then run$ pip freeze > installed_modules.txton the first system to get a list of the installed modules in the installed_modules.txt file. Now copy this file over to second system. Now use pip to install these modules using:$ pip install -r installed_modules.txtThis will install all modules that were installed on the first system. It is ... Read More

6K+ Views
In Python, there are several ways to import modules without requiring installation. This can be particularly useful when you do not have administrative privileges or need to manage different module versions. Below are some common approaches: Using 'sys.path' to Include Additional Directories Using 'virtualenv' for Isolated Environments Using 'importlib' for Dynamic Imports Using 'sys.path' to Include Additional Directories We can add directories to Python's search path at runtime using the sys.path list. This allows Python to look for modules in custom locations and include directories where ... Read More
1K+ Views
What is Variable Scope in Python? Variable scope in Python defines where a variable can be assigned or modified in your code. It determines the visibility and lifetime of variables, controlling which parts of your program can use particular variables. Variable scope in Python provides many benefits, which include - To avoid naming conflicts To manage memory efficiently To write more maintainable and modular code To prevent unnecessary variable modifications Python has four main levels of variable scope, which is the LEGB ... Read More

564 Views
You connect and use a python module on the remote computer over SSH, as SSH only provides limited functionality so calling the module isn't possible.You can call a script on the remote server and run that as a way of getting around this problem. To get a result from the script, you can look at it by reading the lines from stdout if you're logging your result. Alternatively, you can write the result to a file and then read the file once the result has been generated and written to the file.If you want to do this over the network ... Read More

1K+ Views
You can use the jsmin module to minimize/obfuscate javascript code using Python. Install jsmin using:$ pip install jsminTo use jsmin in your python project to minimize a js file, say hello.js, you can use it as follows:>>> from jsmin import jsmin >>> with open('hello.js') as js_file: ... minified = jsmin(js_file.read()) >>> print minifiedYou'll get the minified JS code printed to your shell. You can also use jsmin as a command line tool:$ python -m jsmin hello.jsYou can read more about jsmin on pypi docs: https://pypi.python.org/pypi/jsmin