python-import

Does Python import statement also import dependencies automatically?

那年仲夏 提交于 2020-01-10 04:14:46
问题 I have the following file app.py class Baz(): def __init__(self, num): self.a = num print self.a def foo(num): obj = Baz(num) and the second file main.py from app import foo foo(10) Running the file python main.py gives the correct output. Now in the second file, I'm just importing the function not the class, although successful execution of my function requires the class as well. When importing the function does Python automatically import everything else which is needed to run that function

How to use OpenCV in Python?

别等时光非礼了梦想. 提交于 2020-01-09 09:10:09
问题 I have just installed OpenCV on my Windows 7 machine. As a result, I get a new directory: C:\OpenCV2.2\Python2.7\Lib\site-packages In this directory, I have two files: cv.lib and cv.pyd . Then I try to use the opencv from Python. I do the following: import sys sys.path.append('C:\OpenCV2.2\Python2.7\Lib\site-packages') import cv As a result I get the following error message: File "<stdin>", line 1, in <module> ImportError: DLL load failed: The specified module could not be found. What am I

How does python find a module file if the import statement only contains the filename?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-08 16:53:15
问题 Everywhere I see Python code importing modules using import sys or import mymodule How does the interpreter find the correct file if no directory or path is provided? 回答1: http://docs.python.org/3/tutorial/modules.html#the-module-search-path 6.1.2. The Module Search Path When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path . sys

How to import a Python class that is in a directory above?

不羁的心 提交于 2020-01-08 11:16:48
问题 I want to inherit from a class in a file that lies in a directory above the current one. Is it possible to relatively import that file? 回答1: Inside a package hierarchy, use two dots, as the import statement doc says: When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using

Python imports with __init__.py

元气小坏坏 提交于 2020-01-07 03:59:20
问题 No matter how I structure the imports in the code files and in the __init__.py files, I can't seem to get it right for executing the program and running the tests using pytest . How do I need to write the imports when my project structure looks like this: src/ __init__.py VocableFileWriter.py WordListHelper.py WordListReader.py XLDAttributeValueAdder.py exceptions/ __init__.py XMLInvalidException.py XMLParseException.py gui/ __init__.py GTKSignal.py XLDAttributeValueAdderWindow.py test/ _

How do I pass a value to another file, which is itself imported in current file?

╄→尐↘猪︶ㄣ 提交于 2020-01-07 03:22:13
问题 I have two files, one containing the tkinter code and another containing a function. I have a button in the tkinter window and an Entry field. I am trying to execute the function when clicking the button, but it needs the text from the Entry field to work. I get an error when trying to import anything from the tkinter file: tkinter_file.py: import File window = Tk() def input(): s = entry1.get() return s entry1 = Entry(window) button1 = Button(window, text='GO', command=File.function) File.py

load py file from path/folder

喜欢而已 提交于 2020-01-07 02:59:05
问题 I busy with some calculations in Python and therefore i have some bunch of scripts. I have tried to clean this up thru 2 folders named scripts and tests . Now i have the problem that my main Python file don't recognize the scripts in the subfolders. So my import filename don't work anny more. When i look in some git files it looks like the don mention paths and still it works. I had looked at this SE question but that gave me a error ( ImportError: No module named "filename" ) What have i to

Why is calling `import random; random.random()` throwing a TypeError?

落爺英雄遲暮 提交于 2020-01-06 20:34:48
问题 I'm new to Python. I wrote a small script to generate ten random float values, but it keeps failing. Here's the script: import random for i in range(10): x = random.random() print x and here's the error message: TypeError: 'module' object is not callable'. I can't see what the problem is. I'm pretty sure random exists! My version is Python 2.7.6. 回答1: If you save the following code into random.py : import random print(__name__) print(random) print(random.random) and run it then it prints

Python 'cannot import name'

穿精又带淫゛_ 提交于 2020-01-06 13:52:56
问题 So i have structure like this And i want to import userlist.py from server.py. Server py: import socket from server import userlist #<-- error here class Server(): def __init__(self,port): Other files in server don't have any refferences to userlist . It says cannot import name userlist If i change import to from server.userlist import Userlist it says: 'server' is not a package 回答1: Try just import userlist When you are importing a file (module) in the same directory, all you need to do is

Python 'cannot import name'

邮差的信 提交于 2020-01-06 13:52:22
问题 So i have structure like this And i want to import userlist.py from server.py. Server py: import socket from server import userlist #<-- error here class Server(): def __init__(self,port): Other files in server don't have any refferences to userlist . It says cannot import name userlist If i change import to from server.userlist import Userlist it says: 'server' is not a package 回答1: Try just import userlist When you are importing a file (module) in the same directory, all you need to do is