python-import

Python imports for tests using nose - what is best practice for imports of modules above current package

浪尽此生 提交于 2019-12-28 04:51:23
问题 This is a question which is asked frequently in different forms, and often obtains "lol you're not doing it properly" responses. Pretty sure that's because there's a common sense scenario people (including me) are trying to use as an implementation, and the solution is not obvious (if you've not done it before). Would accept an answer which "lets the fly out of the bottle". Given project/ __init__.py /code __init__.py sut.py /tests __init__.py test_sut.py Where tests_sut.py starts: import

Using Two Python Libraries with Conflicting Names

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-28 04:04:23
问题 I want to use two Python libraries (Google's Cloud Library, and their Cloud SDK) in a single application, but they have conflicting names (they both use google in their base import names and do not use relative imports internally). How can I use them in a single app? Changing the library's code to use proper relative imports is not practical. Also, I know I can use virtualenv to access these libraries from separate python applications, but how do I access them from within the same python app?

What are good rules of thumb for Python imports?

蹲街弑〆低调 提交于 2019-12-28 02:22:11
问题 I am a little confused by the multitude of ways in which you can import modules in Python. import X import X as Y from A import B I have been reading up about scoping and namespaces, but I would like some practical advice on what is the best strategy, under which circumstances and why. Should imports happen at a module level or a method/function level? In the __init__.py or in the module code itself? My question is not really answered by "Python packages - import by class, not file" although

Is there are a way to replace python import with actual sources?

*爱你&永不变心* 提交于 2019-12-26 03:55:29
问题 I am having python files with the import statements which I would like to replace into the actual code placed in the foo.py. For instance, with the in file: from foo import Bar bar = Bar() print bar I would like to out file below: # source of Bar class. bar = Bar() print bar How can I perform such imports replacement ? 回答1: I suggest you to use the ast.NodeTransformer in order to accomplish such import replacement. AST provides way to interact with the python code as with the trees of the

ImportError After Install of lxml on OS X 10.6.8

↘锁芯ラ 提交于 2019-12-25 11:51:25
问题 I tried installing lxml on OS X 10.6.8 by: Downloading the source from PyPi sudo python setup.py build --static-deps sudo python setup.py install It installed fine, without any errors. And when I go to into the Python REPL I get the following: Nabs$ python Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 14:13:39) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import lxml >>> import lxml.html Traceback (most recent call

What is the default binding to the `__import__` attribute of the module `builtin`?

时光总嘲笑我的痴心妄想 提交于 2019-12-25 08:59:16
问题 From Python in a Nutshell Custom Importers An advanced, rarely needed functionality that Python offers is the ability to change the semantics of some or all import and from statements. Rebinding __import__ You can rebind the __import__ attribute of the module builtin to your own custom importer function—for example, one using the generic built-in-wrapping technique shown in “Python built-ins” on page 174. In "You can rebind the __import__ attribute of the module builtin ", should "the module

How to make `from . import utils` work

限于喜欢 提交于 2019-12-25 08:48:27
问题 I have the following directory structure: some-tools-dir/ base_utils.py other_utils.py some-tool.py some-other-tool.py some-other-tools-dir/ basetools -> symlink to ../some-tools-dir yet-another-tool.py In other_utils.py , I have: import base_utils Now, in yet-another-tool.py , I want to do: import basetools.other_utils That doesn't work, because Python does not recognize basetools as a Python package. So I add an empty basetools/__init__.py . Now, in other_utils , I get the exception: import

Import error: No module named tkinter [duplicate]

人盡茶涼 提交于 2019-12-25 06:48:10
问题 This question already has answers here : ImportError: No module named 'Tkinter' (6 answers) Closed 3 years ago . I'm attempting to build graphics for a calculator I made, but I'm getting an error saying that there's no module tkinter. How do I fix my code so that it correctly imports tkinter? import sys from tkinter import* root = Tk() frame = Frame(root) frame.pack() root.title('Calculator') root.mainloop() 回答1: Check what python version you have If you have python 3x: from tkinter import *

Circular dependency in models with sqlalchemy

帅比萌擦擦* 提交于 2019-12-25 06:02:21
问题 I'm creating a webapp which uses SQLAlchemy to access the database. I'm getting stuck with two models which are referencing to each other and ending up in an circular import which throws the following exception: --- SNIPP --- (output modified to hide software name) File "/opt/promethium/service/opt/xxxx/lib/python/api/server/server.py", line 11, in <module> from api.server.controllers import * File "/opt/promethium/service/opt/xxxx/lib/python/api/server/controllers/__init__.py", line 3, in

Circular dependency in models with sqlalchemy

风格不统一 提交于 2019-12-25 06:02:08
问题 I'm creating a webapp which uses SQLAlchemy to access the database. I'm getting stuck with two models which are referencing to each other and ending up in an circular import which throws the following exception: --- SNIPP --- (output modified to hide software name) File "/opt/promethium/service/opt/xxxx/lib/python/api/server/server.py", line 11, in <module> from api.server.controllers import * File "/opt/promethium/service/opt/xxxx/lib/python/api/server/controllers/__init__.py", line 3, in