Questions tagged [python-import]

For questions about importing modules in Python

Importing modules with python:

  • import random

You can use functions from the random module by writing random. in front of the function, for example random.randint

  • import random as rand

The same as above but instead writing rand. before the function

  • from random import ...

imports specific functions, or * for all into your program, you can call the functions as if they were in the file without writing anything before them.

4828 questions
168
votes
12 answers

Why is "import *" bad?

It is recommended to not to use import * in Python. Can anyone please share the reason for that, so that I can avoid it doing next time?
Software Enthusiastic
  • 21,163
  • 15
  • 55
  • 66
159
votes
8 answers

How to mock an import

Module A includes import B at its top. However under test conditions I'd like to mock B in A (mock A.B) and completely refrain from importing B. In fact, B isn't installed in the test environment on purpose. A is the unit under test. I have to…
Jonathan
  • 84,911
  • 94
  • 244
  • 345
157
votes
23 answers

No module named _sqlite3

I am trying to run a Django app on my VPS running Debian 5. When I run a demo app, it comes back with this error: File "/usr/local/lib/python2.5/site-packages/django/utils/importlib.py", line 35, in import_module __import__(name) File…
Alexander van Dijk
  • 1,671
  • 2
  • 11
  • 3
151
votes
5 answers

Can you define aliases for imported modules in Python?

In Python, is it possible to define an alias for an imported module? For instance: import a_ridiculously_long_module_name ...so that is has an alias of 'short_name'.
Jordan Parmer
  • 32,842
  • 27
  • 93
  • 118
150
votes
5 answers

adding directory to sys.path /PYTHONPATH

I am trying to import a module from a particular directory. The problem is that if I use sys.path.append(mod_directory) to append the path and then open the python interpreter, the directory mod_directory gets added to the end of the list sys.path.…
UnadulteratedImagination
  • 1,695
  • 2
  • 12
  • 14
149
votes
6 answers

Should I use `import os.path` or `import os`?

According to the official documentation, os.path is a module. Thus, what is the preferred way of importing it? # Should I always import it explicitly? import os.path Or... # Is importing os enough? import os Please DON'T answer "importing os works…
Denilson Sá Maia
  • 40,640
  • 31
  • 100
  • 109
147
votes
1 answer

Why does using from __future__ import print_function breaks Python2-style print?

I am new at programming with python, and I am trying to print out with a separator and end but it is still giving me a syntax error. I am using python 2.7. Here is my code: from __future__ import print_function import sys, os, time for x in…
UHMIS
  • 1,591
  • 2
  • 9
  • 5
140
votes
8 answers

Visibility of global variables in imported modules

I've run into a bit of a wall importing modules in a Python script. I'll do my best to describe the error, why I run into it, and why I'm tying this particular approach to solve my problem (which I will describe in a second): Let's suppose I have a…
Nubarke
  • 1,574
  • 2
  • 10
  • 9
127
votes
17 answers

Check if Python Package is installed

What's a good way to check if a package is installed while within a Python script? I know it's easy from the interpreter, but I need to do it within a script. I guess I could check if there's a directory on the system that's created during the…
Kevin
  • 1,943
  • 2
  • 18
  • 20
125
votes
9 answers

ImportError: libSM.so.6: cannot open shared object file: No such file or directory

When trying to import OpenCV, using import cv2 I get the following error: /usr/local/lib/python2.7/dist-packages/cv2/__init__.py in () 7 8 # make IDE's (PyCharm) autocompletion happy ----> 9 from .cv2 import * 10 11 #…
Dmitry Rastorguev
  • 2,995
  • 4
  • 11
  • 14
124
votes
9 answers

Python Nose Import Error

I can't seem to get the nose testing framework to recognize modules beneath my test script in the file structure. I've set up the simplest example that demonstrates the problem. I'll explain it below. Here's the the package file…
halfak
  • 1,676
  • 2
  • 12
  • 17
118
votes
5 answers

When to use os.name, sys.platform, or platform.system?

As far as I know, Python has 3 ways of finding out what operating system is running on: os.name sys.platform platform.system() Knowing this information is often useful in conditional imports, or using functionality that differs between platforms…
ztangent
  • 1,571
  • 2
  • 12
  • 11
114
votes
12 answers

Python: Best way to add to sys.path relative to the current running script

I have a directory full of scripts (let's say project/bin). I also have a library located in project/lib and want the scripts to automatically load it. This is what I normally use at the top of each script: #!/usr/bin/python from os.path import…
James Harr
  • 1,497
  • 2
  • 11
  • 9
112
votes
5 answers

List all the modules that are part of a python package?

Is there a straightforward way to find all the modules that are part of a python package? I've found this old discussion, which is not really conclusive, but I'd love to have a definite answer before I roll out my own solution based on os.listdir().
static_rtti
  • 46,349
  • 44
  • 123
  • 180
109
votes
11 answers

Unresolved Import Issues with PyDev and Eclipse

I am very new to PyDev and Python, though I have used Eclipse for Java plenty. I am trying to work through some of the Dive Into Python examples and this feels like an extremely trivial problem that's just becoming exceedingly annoying. I am using…
ely
  • 63,678
  • 30
  • 130
  • 206