Rajendra Dharmkar has Published 189 Articles

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

Rajendra Dharmkar

Rajendra Dharmkar

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

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

How to install and import Python modules at runtime?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 11:18:18

2K+ Views

You can use pip to install packages at runtime and importlib.import_module(moduleName) to import module by using its name as a string. For example, import pip import importlib def import_with_auto_install(package):     try:         return importlib.import_module(package)     except ImportError:         pip.main(['install', package])     ... Read More

How to develop programs with Python Namespaced Packages?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 11:17:05

257 Views

In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories Package-1 and Package-2 in PYTHONPATH, Package-1/namespace/__init__.py Package-1/namespace/module1/__init__.py Package-2/namespace/__init__.py Package-2/namespace/module2/__init__.py the end-user can import namespace.module1 and import namespace.module2.On Python ... Read More

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

Rajendra Dharmkar

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

How to install python modules and their dependencies easily?

Rajendra Dharmkar

Rajendra Dharmkar

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

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

588 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

How do I get IntelliJ to recognize common Python modules?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 06:55:23

581 Views

To make IntelliJ to recognize common Python modules, just create and add Python SDKFile -> Project Structure -> Project -> Project SDK -> newand select the installation path of your Python interpreter (for example, C:\Python26 in windows and /usr/bin/python2.7 in Linux) as the home path. This allows IntelliJ to look ... Read More

How to set up your python development environment on AWS?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 06:45:55

428 Views

You need to have Python, pip, virtualenv, awswebcli and a SSH client installed to set up your Python Development Environment on AWS. You can follow instructions at http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html to install these.Once you have all those installed, you need to set up a virtual environment so that your global packages do ... Read More

What is the convention for structuring Python modules?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 06:44:20

195 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

Advertisements