python-module

Recursive version of 'reload'

天大地大妈咪最大 提交于 2019-11-26 20:33:41
问题 When I'm developing Python code, I usually test it in an ad-hoc way in the interpreter. I'll import some_module , test it, find a bug, fix the bug and save, and then use the built-in reload function to reload(some_module) and test again. However, suppose that in some_module I have import some_other_module , and while testing some_module I discover a bug in some_other_module and fix it. Now calling reload(some_module) won't recursively re-import some_other_module . I have to either manually

Is it possible to list all functions in a module? [duplicate]

巧了我就是萌 提交于 2019-11-26 19:35:09
问题 This question already has an answer here: How to list all functions in a Python module? 14 answers I defined a .py file in this format: foo.py def foo1(): pass def foo2(): pass def foo3(): pass I import it from another file: main.py from foo import * # or import foo Is it possible list all functions name, e.g. ["foo1", "foo2", "foo3"] ? Thanks for your help, I made a class for what I want, pls comment if you have suggestion class GetFuncViaStr(object): def __init__(self): d = {} import foo

How to use custom classes with Apache Spark (pyspark)?

笑着哭i 提交于 2019-11-26 19:25:43
问题 I have written a class implementing a classifier in python. I would like to use Apache Spark to parallelize classification of a huge number of datapoints using this classifier. I'm set up using Amazon EC2 on a cluster with 10 slaves, based off an ami that comes with python's Anaconda distribution on it. The ami lets me use IPython Notebook remotely. I've defined the class BoTree in a file call BoTree.py on the master in the folder /root/anaconda/lib/python2.7/ which is where all my python

Import python module NOT on path

℡╲_俬逩灬. 提交于 2019-11-26 19:21:39
问题 I have a module foo, containing util.py and bar.py. I want to import it in IDLE or python session. How do I go about this? I could find no documentation on how to import modules not in the current directory or the default python PATH. After trying import "<full path>/foo/util.py" , and from "<full path>" import util The closest I could get was import imp imp.load_source('foo.util','C:/.../dir/dir2/foo') Which gave me Permission denied on windows 7. 回答1: One way is to simply amend your path:

Collapse multiple submodules to one Cython extension

让人想犯罪 __ 提交于 2019-11-26 19:04:57
This setup.py: from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = ( Extension('myext', ['myext/__init__.py', 'myext/algorithms/__init__.py', 'myext/algorithms/dumb.py', 'myext/algorithms/combine.py']) ) setup( name='myext', ext_modules=cythonize(extensions) ) Doesn't have the intended effect. I want it to produce a single myext.so , which it does; but when I invoke it via python -m myext.so I get: ValueError: Attempted relative import in non-package due to the fact that myext attempts to refer to .algorithms . Any idea how

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

时光总嘲笑我的痴心妄想 提交于 2019-11-26 18:47:17
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? dm03514 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.path is initialized from these locations: The directory containing the input script (or the current

Python: What's the difference between __builtin__ and __builtins__?

匆匆过客 提交于 2019-11-26 17:27:43
I was coding today and noticed something. If I open a new interpreter session (IDLE) and check what's defined with the dir function I get this: $ python >>> dir() ['__builtins__', '__doc__', '__name__', '__package__'] >>> dir(__builtins__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt',

Importing modules: __main__ vs import as module

别说谁变了你拦得住时间么 提交于 2019-11-26 16:41:00
To preface, I think I may have figured out how to get this code working (based on Changing module variables after import ), but my question is really about why the following behavior occurs so I can understand what to not do in the future. I have three files. The first is mod1.py: # mod1.py import mod2 var1A = None def func1A(): global var1 var1 = 'A' mod2.func2() def func1B(): global var1 print var1 if __name__ == '__main__': func1A() Next I have mod2.py: # mod2.py import mod1 def func2(): mod1.func1B() Finally I have driver.py: # driver.py import mod1 if __name__ == '__main__': mod1.func1A()

Python: 'Private' module in a package

冷暖自知 提交于 2019-11-26 16:13:04
问题 I have a package mypack with modules mod_a and mod_b in it. I intend the the package itself and mod_a to be imported freely: import mypack import mypack.mod_a However, I'd like to keep mod_b for the exclusive use of mypack . That's because it exists merely to organize the latter's internal code. My first question is, is it an accepted practice in Python programming to have 'private' modules like this? If yes, my second question is, what is the best way to convey this intention to the client?

Can modules have properties the same way that objects can?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 15:49:41
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 stored there. Alex Martelli Only instances of new-style classes can have properties. You can make Python believe such an instance is a module by stashing it in sys.modules[thename] = theinstance . So, for example, your m.py module file could be: import sys class _M(object): def __init__(self): self.c = 0 def afunction(self): self.c += 1 return self.c y =