To view the paths of an imported Python module, say
# Output is:
This can be useful to check if the right module from the right location is being imported. It is also useful to know where to go check out the Python source files of a module for inspection or debugging.
I was trying to install the latest NVIDIA graphics driver on a machine. When I ran the installer, it would complain like this:
An NVIDIA kernel module nvidia appears to already be loaded in your kernel
I tried to remove the module:
$ sudo modprobe -r nvidia
But, the kernel complained that the module was being used.
I tried to see which process was using this module:
$ lsof /dev/nvidia*
This showed that no process was using the module. This was surprising.
This is what worked for me in the end. I added a
blacklist nvidia line to
/etc/modprobe.d/blacklist.conf and rebooted the machine.
I then invoked the NVIDIA driver installer with the
--uninstall option. Another reboot later, I found that the module was no longer in the kernel.
I was able to install the NVIDIA driver after that.
Tried with: Ubuntu 14.04
It is fairly common in many applications to read or write series of bits into bytes or longer array of bytes. For example, to store results of various binary tests. The space saved by storing these results as bits might be crucial when memory or disk resources are the bottleneck.
If you deal with an array of bits, the Python module you need is bitarray.
Installing it for Python 2.x is simple:
$ sudo apt install python-bitarray
If you are using Python 3.x:
$ sudo apt install python3-bitarray
Creating an empty bitarray is easy:
ba = bitarray.bitarray()
Note that the endianess of the data is very important in these applications. bitarray uses big-endian interpretation by default. If your data is stored in little-endian, then specify this explicitly:
ba = bitarray.bitarray(endian="little")
You can check the endianess of any bitarray:
To initialize a bitarray from a byte string:
To get or set a specific bit in the bitarray:
ba = 0
Tried with: Bitarray 0.8.0, Python 3.4 and Ubuntu 14.04
Every Python source file can essentially be considered as a module. The following rule applies to the name of a module, if it needs to be imported without problems:
(letter|"_") (letter | digit | "_")*
That is, the rule for naming a Python file is the same as naming a variable in it. First character has to be letter or underscore, followed by any number of letters, digits or underscores. No other characters, like dash, are allowed.
If the file is not named correctly, the following error or warning might be generated on importing it:
Invalid name for Python module
Tried with: Python 2.7.6 and Ubuntu 14.04
CPAN hosts thousands of Perl modules that can be installed easily on your computer. The traditional method to install a module named foo::bar is by using the cpan command:
$ cpan foo::bar
However, the tool asks you to answer quite a few questions to go through with the module installation.
A highly recommended improvement to CPAN module installation is to use cpanminus.
The easiest way to install it is from your distribution package:
$ sudo apt install cpanminus
You can also install it directly from CPAN:
$ cpan App::cpanminus
After this, you can install any CPAN module by calling cpanm:
$ cpanm foo::bar
The advantage of using cpanminus is that it takes care of automating the module installation process as much as possible for you.
Tried with: CPAN 1.61 and Ubuntu 16.04
The Python program has a useful parameter: -v When Python is launched with this parameter to run a Python program, it prints out all the Python modules and dynamic libraries that are loaded during the executing. This can be useful to see exactly which Python files and shared library files are loaded and in which order. This parameter also prints out the modules as they are cleaned up at the end of program execution.
$ python -v foo.py
Tried with: Python 2.7.3 and Ubuntu 12.04 LTS
Once you start installing many Python modules, you are sure to run into this error. When installing a Python module, it pops up this error:
Python version 3.2 required, which was not found in the registry.
I do have Python 3.2 installed, so what’s up?
The problem is that some module installers look in the wrong place for Python version information. For example, in the case of this one module, the installer was looking for
HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.2 in the registry. I found that my Python 3.2 installer had placed this information in
Once you know the reason, solving it is easy. Open Registry Editor (may need administrator privilege) and export
HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.2 into a text file. Open the exported
.reg file in a text editor and replace the
HKEY_LOCAL_MACHINE entries to
HKEY_CURRENT_USER. Add these new entries back to the registry (here is how). The module installer should now be able to proceed without throwing the error.
Tried with: Python 3.2 64-bit on Windows 7