Found 34494 Articles for Programming

How can I import modules for a Python Azure Function?

Rajendra Dharmkar
Updated on 17-Feb-2020 07:41:36

921 Views

As of writing this, Python support for Azure Functions is experimental. So right now there is no way to directly get a module from a package manager to be installed on your instance. You'll need to bring your own modules with code. No modules are available by default on Azure Functions. You can add them by uploading it via the portal UX or kudu (which is handy for lots of files).If you don't mind using virtualenv, there is an alternative.Create your python script on Azure Functions.Open a Kudu console and cd to your script location.Create a virtualenv in this folder ... Read More

How to check if a python module exists without importing it?

Rajendra Dharmkar
Updated on 01-Oct-2019 11:21:15

767 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

What are the most useful Python modules from the standard library?

Sarika Singh
Updated on 14-Nov-2022 07:30:09

6K+ 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

How do I calculate the date six months from the current date using the datetime Python module?

Sarika Singh
Updated on 14-Nov-2022 08:09:49

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. This article tells about how to display the current date by importing the datetime module. Using relativedelta() function The relativedelta type is intended to be applied to an existing datetime and can either indicate a period of time or replace specific elements of that datetime. Example The Python datetime module can be used to get the date six months from the current date. The code is shown below − from datetime import date from dateutil.relativedelta import relativedelta ... Read More

How to prohibit a Python module from calling other modules?

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

159 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

Is it possible to use Python modules in Octave?

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

180 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)

How we can copy Python modules from one system to another?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:50:57

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

How we can import Python modules without installing?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:51:33

4K+ Views

Yes there are ways to import Python modules without installing. If you are not able to install modules on a machine(due to not having enough permissions), you could use either virtualenv or save the module files in another directory and use the following code to allow Python to search for modules in the given module:>>> import os, sys >>> file_path = 'AdditionalModules/' >>> sys.path.append(os.path.dirname(file_path)) >>> # Now python also searches AdditionalModules folder for importing modules as we have set it on the PYTHONPATH.You can also use virtualenv to create an isolated local Python environment. The basic problem being addressed is ... Read More

How does variable scopes work in Python Modules?

Pranathi M
Updated on 16-Sep-2022 07:34:35

1K+ Views

The scope of the Python object determines its accessibility. The scope must be specified in order to access the particular variable in the code because it cannot be accessible from anywhere in the program. The term "scope" describes the precise coding region where variables are shown. It is possible to limit the visibility of variables so that only certain people can see them. Scope confirms which variable can be "Seen". The scope determines the rules that regulate how and where a variable can be searched. The variable is searched either to assign a value or to retrieve one. The namespace ... Read More

How to use Python modules over Paramiko (SSH)?

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

423 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

Advertisements