Questions tagged [python-module]

A module is a file containing Python definitions and statements.

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

2038 questions
8066
votes
46 answers

What does if __name__ == "__main__": do?

What does this do, and why should one include the if statement? if __name__ == "__main__": print("Hello, World!") If you are trying to close a question where someone should be using this idiom and isn't, consider closing as a duplicate of Why…
Devoted
  • 177,705
  • 43
  • 90
  • 110
1747
votes
35 answers

How can I import a module dynamically given the full path?

How do I load a Python module given its full path? Note that the file can be anywhere in the filesystem where the user has access rights. See also: How to import a module given its name as string?
derfred
  • 18,881
  • 3
  • 23
  • 25
1211
votes
23 answers

How do I import other Python files?

How do I import files in Python? I want to import: a file (e.g. file.py) a folder a file dynamically at runtime, based on user input one specific part of a file (e.g. a single function)
Tamer
  • 12,137
  • 3
  • 16
  • 4
794
votes
17 answers

TypeError: 'module' object is not callable

File "C:\Users\Administrator\Documents\Mibot\oops\blinkserv.py", line 82, in __init__ self.serv = socket(AF_INET,SOCK_STREAM) TypeError: 'module' object is not callable Why am I getting this error? I'm confused. How can I solve this error?
user551717
  • 8,217
  • 5
  • 18
  • 9
611
votes
18 answers

How can I do relative imports in Python?

Imagine this directory structure: app/ __init__.py sub1/ __init__.py mod1.py sub2/ __init__.py mod2.py I'm coding mod1, and I need to import something from mod2. How should I do it? I tried from ..sub2 import mod2,…
Joril
  • 19,961
  • 13
  • 71
  • 88
497
votes
10 answers

How do I find out my PYTHONPATH using Python?

How do I find out which directories are listed in my system’s PYTHONPATH variable, from within a Python script (or the interactive shell)?
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
490
votes
13 answers

How can I Install a Python module within code?

I need to install a package from PyPI straight within my script. Is there maybe some module or distutils (distribute, pip, etc.) feature which allows me to just execute something like pypi.install('requests') and requests will be installed into my…
chuwy
  • 6,310
  • 4
  • 20
  • 29
428
votes
7 answers

How to write a Python module/package?

I've been making Python scripts for simple tasks at work and never really bothered packaging them for others to use. Now I have been assigned to make a Python wrapper for a REST API. I have absolutely no idea on how to start and I need help. What I…
yowmamasita
  • 4,810
  • 3
  • 17
  • 17
235
votes
39 answers

Unable to import a module that is definitely installed

After installing mechanize, I don't seem to be able to import it. I have tried installing from pip, easy_install, and via python setup.py install from this repo: https://github.com/abielr/mechanize. All of this to no avail, as each time I enter my…
roy
  • 3,706
  • 6
  • 30
  • 53
148
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
  • 3,473
  • 4
  • 13
  • 14
131
votes
8 answers

Can modules have properties the same way that objects can?

With python properties, I can make it such that obj.y calls a function rather than just returning a value. Is there a way to do this with modules? I have a case where I want module.y to call a function, rather than just returning the value…
Josh Gibson
  • 21,808
  • 28
  • 67
  • 63
116
votes
17 answers

ImportError: cannot import name '...' from partially initialized module '...' (most likely due to a circular import)

I'm upgrading an application from Django 1.11.25 (Python 2.6) to Django 3.1.3 (Python 3.8.5) and, when I run manage.py makemigrations, I receive this messasge: File "/home/eduardo/projdevs/upgrade-intra/corporate/models/section.py", line 9, in…
3WZ
  • 1,165
  • 2
  • 7
  • 5
91
votes
3 answers

Importing a library from (or near) a script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError

I have a script named requests.py that needs to use the third-party requests package. The script either can't import the package, or can't access its functionality. Why isn't this working, and how do I fix it? Trying a plain import and then using…
idjaw
  • 25,487
  • 7
  • 64
  • 83
89
votes
7 answers

What exactly is a "raw string regex" and how can you use it?

From the python documentation on regex, regarding the '\' character: The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
87
votes
10 answers

How to make global imports from a function?

I fear that this is a messy way to approach the problem but... let's say that I want to make some imports in Python based on some conditions. For this reason I want to write a function: def conditional_import_modules(test): if test == 'foo': …
Giovanni Di Milia
  • 13,480
  • 13
  • 55
  • 67
1
2 3
99 100