Found 10784 Articles for Python

How to Install two python modules with same name?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:43:14

2K+ Views

This is not possible with the pip. All of the packages on PyPI have unique names. Packages often require and depend on each other, and assume the name will not change. Even if you manage to put the code on Python path, when importing a module, python searches the paths in sys.path by order and stops at first match. So whatever module it finds first, it'll stop at that. You best bet is to copy all the code from the libraries to you codebase, change the module name of either and then import it.If you're importing modules with same name from ... Read More

Can we keep Python modules in compiled format?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:44:19

438 Views

Yes you can keep Python modules in compiled format. Python automatically compiles Python source code when you import a module, so the easiest way to create a PYC file is to import it. If you have a module mymodule.py, just do:>>> import mymoduleto create a mymodule.pyc file in the same directory. A drawback is that it doesn’t only compile the module, it also executes it, which may not be what you want. (however, it does compile the entire script even if it fails to execute the script). To do this programmatically, and without executing the code, you can use the ... Read More

How to install python modules and their dependencies easily?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:46:23

994 Views

The best and recommended way to install Python modules is to use pip, the Python package manager. It automatically installs dependencies of the module as well.If you have Python 2 >=2.7.9 or Python 3 >=3.4 installed from python.org, you will already have pip and setup tools, but will need to upgrade to the latest version:On Linux or macOS:pip install -U pip setuptoolsOn Windows:python -m pip install -U pip setuptoolsIf you’re using a Python install on Linux that’s managed by the system package manager (e.g "yum", "apt-get" etc…), and you want to use the system package manager to install or upgrade ... Read More

How to use remote python modules?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:47:54

2K+ Views

There are ways to import Python modules remotely. It is not recommended to do so though as it will slow down your app. You can use the knockout module to achieve this. To install knockout use:$ pip install knockoutNow in order to import modules remotely, you can use knockout like:>>> from knockout import urlimport >>> urlimport.register() Url importing enabled. Add urls to sys.path.A valid url looks like this: http://example.com/path/to/repository/#packagenameThis stuff is experimental, use at your own risk. Enjoy.>>> import sys >>> sys.path.insert(0, 'http://www.crummy.com/software/BeautifulSoup/download/3.x/BeautifulSoup-3.0.8/#BeautifulSoup') >>> import BeautifulSoup ... >>> BeautifulSoup If you are not able to install modules on a machine(due ... Read More

How to call a function of a module from a string with the function's name in Python?

Rajendra Dharmkar
Updated on 11-Dec-2019 09:25:32

787 Views

Objects in python have instance variables and methods as attributes. To call a function of a module from a string with the function's name in Python, you can get this attribute first and call the function object attached to it. For example, let's say that I have a module foo, and I have a string whose contents are "bar". The best way to go about calling foo.bar() is:>>> import foo >>> method_to_call = getattr(foo, 'bar') >>> result = method_to_call()If you have the function in global or local namespace without the module prefix, you can also use globals()/locals(). locals returns a ... Read More

How I can install unidecode python module on Linux?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:49:13

1K+ Views

To install unidecode or any other python module you need pip installed(python package manager). If you have Python 2 >=2.7.9 or Python 3 >=3.4 installed from python.org, you will already have pip and setuptools, but will need to upgrade to the latest version:On Linux or macOS:pip install -U pip setuptoolsOn Windows:python -m pip install -U pip setuptoolsIf you’re using a Python install on Linux that’s managed by the system package manager (e.g "yum", "apt-get" etc…), and you want to use the system package manager to install or upgrade pip, then see: https://packaging.python.org/guides/installing-using-linux-tools/Otherwise:Download get-pip.py from https://bootstrap.pypa.io/get-pip.py. Run python get-pip.py. This will ... Read More

Is there something available in Python like PHP autoloader?

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

239 Views

No there is not. And you should not try something like that in Python. There's a good reason for autoloading in PHP which is that PHP scripts are executed from scratch every time you load some page. The opcodes may be cached and files may be already prepared, but the main point remains - importing another file actually means making sure its contents are loaded.In Python webservices, your files are not reread every time. If you import something, its imported for the lifetime of the app. Autoloaders in Python would be used only one time, not on each request.So autoloaders ... Read More

How to develop a Python Module?

Pranathi M
Updated on 16-Sep-2022 07:19:09

442 Views

A file containing Python commands and definitions is referred to as a module. These files named .py which contain that contains Python code, such as example.py, and the name of the module is an example. Modules are used to divide down huge applications into smaller, more manageable files. The prerequisites for using modules you should have Python 3 installed and a programming environment set up. If you don't already have one, you can refer to the installation and setup recommendations for a local programming environment. Example 1 Let us look at an example to create a simple python module. Let ... Read More

Where are the python modules stored?

Rajendra Dharmkar
Updated on 01-Oct-2019 07:05:04

3K+ Views

Python Modules are usually stored in /lib/site-packages in your Python folder. If you want to see what directories Python checks when importing modules, you can log the following:>>> import sys >>> print sys.path ['', 'C:\Python27', 'C:\Python27\Lib\site-packages', 'C:\Python27\Lib', 'C:\Python27\DLLs', 'C:\Python27\Lib\lib-tk', 'C:\Python27\Scripts', 'C:\WINDOWS\SYSTEM32\python27.zip', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\site-packages\win32', 'C:\Python27\lib\site-packages\win32\lib', 'C:\Python27\lib\site-packages\Pythonwin']You can also list where individual modules being used by a script(say hello.py) reside on the system using the python -v command. For example, $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py ... Read More

How do you dynamically add Python modules to a package while your programming is running?

Rajendra Dharmkar
Updated on 01-Oct-2019 07:06:04

371 Views

To dynamically import Python modules, you can use the importlib package's import_module(moduleName) function. You need to have moduleName as a string. For example, >>> from importlib import import_module >>> moduleName = "os" >>> globals()[moduleName] = import_module(moduleName)If you want to dynamically import a list of modules, you can even call this from a for loop. For example, >>> import importlib >>> modnames = ["os", "sys", "math"] >>> for lib in modnames: ...     globals()[lib] = importlib.import_module(lib)The globals() call returns a dict. We can set the lib key for each library as the object returned to us on import of a ... Read More

Advertisements