Found 9192 Questions for Python

What are common practices for modifying Python modules?

Rajendra Dharmkar
Updated on 11-Dec-2019 09:59:06
If you are modifying a module and want to test it in the interpreter without having to restart the shell everytime you save that module, you can use the reload(moduleName) function. reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "import moduleName". It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again.For example>>> import mymodule >>> # Edited mymodule and want to reload it in this script >>> reload(mymodule)Note that the moduleName is the actual name of the ... Read More

What is Python module for date manipulation?

Rajendra Dharmkar
Updated on 11-Dec-2019 10:06:11
Python has a built-in module, datetime which includes functions and classes for doing date and time parsing, formatting, and arithmetic. Time values are represented using time class. It has the attributes for hour, minute, second, and microsecond. They can also include time zone information.For exampleimport datetime t = datetime.time(1, 2, 3) print t print 'hour  :', t.hour print 'minute:', t.minute print 'second:', t.second print 'microsecond:', t.microsecond print 'tzinfo:', t.tzinfoOutputThis will give you the output:$ python datetime_time.py 01:02:03 hour  : 1 minute: 2 second: 3 microsecond: 0 tzinfo: NoneA time instance only holds values of the time and not a date ... Read More

How we can import Python modules in Jython?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20
You can use pure python modules from jython. You can't use modules that are implemented in C. To use modules from your pip installs, you need to add the sys.path of python to that of Jython as Jython does not automatically pick up the PYTHONPATH informationJython 2.5 introduced the JYTHONPATH environmental variable as Jython-equivalent of PYTHONPATH, so setting both to the same value should do the trick for most use cases (unless you're working in a setup with incompatible Python an Jython versions).Now you can import python modules installed locally directly using 'import' in Jython.

How to Install two python modules with same name?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:43:14
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
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 import Python modules by default at the time of program starts?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20
Use the environment variable PYTHONSTARTUP. From the official documentation:If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.So, just create a python script with the import statements and point the environment variable to it. For instructions on how to create environment variables on your OS see: https://www.java.com/en/download/help/path.xml. Use the variable name as PYTHONSTARTUP instead ... Read More

How to install python modules and their dependencies easily?

Rajendra Dharmkar
Updated on 01-Oct-2019 10:46:23
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
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
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
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
Advertisements