python-module

How to reload python module imported using `from module import *`

馋奶兔 提交于 2019-11-26 15:43:29
问题 I saw in this useful Q&A that one can use reload(whatever_module) or, in Python 3, imp.reload(whatever_module) . My question is, what if I had said from whatever_module import * to import? Then I have no whatever_module to refer to when I use reload() . Are you guys gonna yell at me for throwing a whole module into the global namespace? :) 回答1: I agree with the "don't do this generally" consensus, but... The correct answer is: import X reload(X) from X import Y # or * for that matter 回答2:

How to get filename of the __main__ module in Python?

与世无争的帅哥 提交于 2019-11-26 15:29:48
问题 Suppose I have two modules: a.py: import b print __name__, __file__ b.py: print __name__, __file__ I run the "a.py" file. This prints: b C:\path\to\code\b.py __main__ C:\path\to\code\a.py Question : how do I obtain the path to the __main__ module ("a.py" in this case) from within the "b.py" library? 回答1: import __main__ print __main__.__file__ 回答2: Perhaps this will do the trick: import sys from os import path print path.abspath(sys.modules['__main__'].__file__) Note that, for safety, you

How to make global imports from a function?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 13:47:24
问题 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': import onemodule, anothermodule elif test == 'bar': import thirdmodule, and_another_module else: import all_the_other_modules Now how can I have the imported modules globally available? For example: conditional_import_modules(test='bar') thirdmodule

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

久未见 提交于 2019-11-26 12:53:34
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 r"\n" is a two-character string containing '\' and 'n' , while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation. What is this raw string notation? If you use a raw string format, does that mean "*" is taken as a a literal character rather than a zero-or-more indicator?

Can't install mysql-python (newer versions) in Windows

萝らか妹 提交于 2019-11-26 12:48:53
问题 I have mysql-python v1.2.4 installed just fine on my machine (Windows 8). I am using Python 2.7. I always got this below error every time I try to upgrade to v1.2.5. (still happens as of v1.3.7) C:\\Users\\User\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\ 9.0\\VC\\Bin\\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,5,\'fi nal\',1) -D__version__=1.2.5 \"-IC:\\Program Files (x86)\\MySQL\\MySQL Connector C 6. 0.2\\include\" -Ic:\\python27\\include -Ic:\

What do I need to read Microsoft Access databases using Python?

邮差的信 提交于 2019-11-26 12:18:57
问题 How can I access Microsoft Access databases in Python? With SQL? I\'d prefere a solution that works with Linux, but I could also settle for Windows. I only require read access. 回答1: I've used PYODBC to connect succesfully to an MS Access db - on Windows though . Install was easy, usage is fairly simple, you just need to set the right connection string (the one for MS Access is given in the list) and of you go with the examples. 回答2: On Linux, MDBTools is your only chance as of now. [disputed]

Python import modules from a higher level package

泪湿孤枕 提交于 2019-11-26 11:34:03
问题 This is my package hierarchy app |--__init__.py //Empty file |--server.py |--global_vars.py | |--handlers |--__init__.py //Empty file | |--url1 | |--__init__.py //Empty file | |--app1.py | |--app2.py | |--url2 |--__init__.py //Empty file |--app3.py Now I want to import global_vars.py inside app1.py . So I gave import app.global_vars.py inside app1.py. But I get the following error: import app.global_vars ImportError: No module named app.global_vars I should also mention that I am importing

How do I extend a python module? Adding new functionality to the `python-twitter` package

萝らか妹 提交于 2019-11-26 11:10:14
问题 What are the best practices for extending an existing Python module – in this case, I want to extend the python-twitter package by adding new methods to the base API class. I\'ve looked at tweepy , and I like that as well; I just find python-twitter easier to understand and extend with the functionality I want. I have the methods written already – I\'m trying to figure out the most Pythonic and least disruptive way to add them into the python-twitter package module, without changing this

Two Python modules require each other's contents - can that work?

无人久伴 提交于 2019-11-26 08:15:23
问题 I have a Bottle webserver module with the following line: from foobar.formtools import auto_process_form_insert And the foobar.formtools module contains this line: from foobar.webserver import redirect, redirect_back Of course, both result in the following errors (respectively): ImportError: cannot import name auto_process_form_insert ImportError: cannot import name redirect Is it simply a fact that in Python two modules can\'t import each other and all module imports must be hierarchical in

How do I find out my python path using python?

萝らか妹 提交于 2019-11-26 06:54:56
问题 How do I find out which directories are listed in my system’s PYTHONPATH variable, from within a Python script (or the interactive shell)? 回答1: sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use: import os try: user_paths = os.environ['PYTHONPATH'].split(os.pathsep) except KeyError: user_paths = [] 回答2: You would probably also want this: import sys print(sys.path) Or as a one liner from the terminal: python -c