python-module

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

两盒软妹~` 提交于 2019-11-27 07:00:51
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 modules are I've checked that I can import and use BoTree.py when running command line spark from the

Define a global in a Python module from a C API

倖福魔咒の 提交于 2019-11-27 06:29:46
问题 I am developing a module for Python using a C API. How can I create a variable that is seen as global from Python? For example, if my module is module , I want to create a variable g that does this job: import module print module.g In particular, g is an integer. Solution from Alex Martelli PyObject *m = Py_InitModule("mymodule", mymoduleMethods); PyObject *v = PyLong_FromLong((long) 23); PyObject_SetAttrString(m, "g", v); Py_DECREF(v); 回答1: You can use PyObject_SetAttrString in your module's

Importing user defined modules in python from a directory

人走茶凉 提交于 2019-11-27 06:15:46
问题 I'm trying to import a module I wrote in python that just prints out a list containing numbers. The issue I'm having is that I want to be able to import it from a separate directory but the answers I have read so far don't seem to be working for my situation. For example, given I want to import printnumbers.py from a directory in my documents folder I am supposed to implement the following: import sys sys.path.append('/home/jake/Documents') import printnumbers.py This snipit of code results

How to debug a Python module run with python -m from the command line?

十年热恋 提交于 2019-11-27 05:48:41
问题 I know that a Python script can be debugged from the command line with python -m pdb my_script.py if my_script.py is a script intended to be run with python my_script.py . However, a python module my_module.py should be run with python -m my_module . Even scripts that contain relative imports should be run with python -m . How can I run python -m my_module under pdb 's control? The following does not work : python -m pdb -m my_module 回答1: You can't do it now, because -m terminates option list

Python import modules from a higher level package

 ̄綄美尐妖づ 提交于 2019-11-27 05:21:41
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 app1.py from server.py. server.py is the file I am actually running. When server.py imports app1.py, app1

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

*爱你&永不变心* 提交于 2019-11-27 04:24:45
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 modules’ core. A few ways. The easy way: Don't extend the module, extend the classes. exttwitter.py import

Error while finding spec for 'fibo.py' (<class 'AttributeError'>: 'module' object has no attribute '__path__')

妖精的绣舞 提交于 2019-11-27 00:43:33
问题 I have a module in a fibo.py file which has the following functions - #fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print(b, end=' ') a, b = b, a+b print() def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result Now when I run the module from the cli python3 as - > python3 -m fibo.py I get the error Error while finding spec for 'fibo.py' (<class 'AttributeError'>: 'module

How is the __name__ variable in a Python module defined?

ⅰ亾dé卋堺 提交于 2019-11-26 22:57:42
问题 I'm aware of the standard example: if you execute a module directly then it's __name__ global variable is defined as "__main__" . However, nowhere in the documentation can I find a precise description of how __name__ is defined in the general case. The module documentation says... Within a module, the module's name (as a string) is available as the value of the global variable __name__ . ...but what does it mean by "the module's name"? Is it just the name of the module (the filename with .py

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

柔情痞子 提交于 2019-11-26 22:19:34
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 nature, or am I doing something wrong? Alternatively, is there a workaround short of placing all these

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

时间秒杀一切 提交于 2019-11-26 22:05:52
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. Alex Boschmans 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. pypyodbc On Linux, MDBTools is your only chance as of now. [disputed] On Windows, you can deal with mdb files with pypyodbc. To create an Access mdb file: