Rajendra Dharmkar has Published 452 Articles

How to install python modules and their dependencies easily?

Rajendra Dharmkar

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 ... Read More

Can we keep Python modules in compiled format?

Rajendra Dharmkar

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 ... Read More

How to Install two python modules with same name?

Rajendra Dharmkar

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 ... Read More

What are the best practices to organize Python modules?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 07:10:31

410 Views

Here is a sample project that shows a very good way to structure your projects: https://github.com/kennethreitz/samplemod. The project is about creating the "sample" module. The directory structure looks as follows:README.rst LICENSE setup.py requirements.txt sample/__init__.py sample/core.py sample/helpers.py docs/conf.py docs/index.rst tests/test_basic.py tests/test_advanced.pyThe README.rst file: This file is for giving a brief description ... Read More

What is the most compatible way to install python modules on a Mac?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 07:09:57

184 Views

The most popular way to manage python packages (if you're not using your system package manager like homebrew) is to use setuptools and easy_install. It is probably already installed on your system. Use it like this:easy_install djangoeasy_install uses the Python Package Index which is an amazing resource for python developers. ... Read More

Can we iteratively import python modules inside a for loop?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 07:08:32

2K+ Views

Yes you can iteratively import python modules inside a for loop. You need to have a list of modules you want to import as strings. You can use the inbuilt importlib.import_module(module_name) to import the modules. For example, >>> import importlib >>> modnames = ["os", "sys", "math"] >>> for lib in ... Read More

How to install python modules without root access?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 07:07:43

683 Views

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 directory:>>> import os, sys >>> file_path ... Read More

How to install libxml2 with python modules on Mac?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 07:07:08

648 Views

You can install libxml2 using the following commands:$ brew install --framework python $ brew install --with-python libxml2 $ brew install --with-python libxslt $ brew link libxml2 --force $ brew link libxslt --forceIf you had already installed libxml2 but it had failed or any other error, you can uninstall it and ... Read More

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

Rajendra Dharmkar

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 ... Read More

Where are the python modules stored?

Rajendra Dharmkar

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 ... Read More

Advertisements