python-module

Define a global in a Python module from a C API

冷暖自知 提交于 2019-11-29 07:49:37
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); You can use PyObject_SetAttrString in your module's initialization routine, with first argument o being (the cast to (PyObject*) of) your module, second

ImportError: No module named backend_tkagg

心已入冬 提交于 2019-11-29 06:32:01
I have such imports and code: import pandas as pd import numpy as np import statsmodels.formula.api as sm import matplotlib.pyplot as plt #Read the data from pydatasets repo using Pandas url = './file.csv' white_side = pd.read_csv(url) #Fitting the model model = sm.ols(formula='budget ~ article_size', data=white_side, subset=white_side['producer'] == "Peter Jackson") fitted = model.fit() print fitted.summary() After execution of this code I have such errors: /usr/bin/python2.7 /home/seth/PycharmProjects/osiris_project/PMN_way/start.py Traceback (most recent call last): File "/home/seth

why __builtins__ is both module and dict

心不动则不痛 提交于 2019-11-29 05:56:17
I am using the built-in module to insert a few instances, so they can be accessed globally for debugging purposes. The problem with the __builtins__ module is that it is a module in a main script and is a dict in modules, but as my script depending on cases can be a main script or a module, I have to do this: if isinstance(__builtins__, dict): __builtins__['g_frame'] = 'xxx' else: setattr(__builtins__, 'g_frame', 'xxx') Is there a workaround, shorter than this? More importantly, why does __builtins__ behave this way? Here is a script to see this. Create a module a.py: #module-a import b print

How To Make Eclipse Pydev Plugin Recognize Newly Installed Python Modules?

两盒软妹~` 提交于 2019-11-29 05:34:45
So I just installed SubnetTree ( http://www.icir.org/robin/pysubnettree/ ) and if I open the Python interactive interpreter I can successfully import it without any error messages. I use it in one of my programs and can successfully run it without a hitch. However, Eclipse marks the import as an error, and this is a problem as I use Eclipse for debugging. I have gone to preferences and have restored the Python interpreter I am using to no avail. I was able to merely restore the Python interpreter exit, and reopen it on my other machine(OS X 10.5, I am now using OS X 10.6) and it identified

Determining the location of distutils data files programmatically in Python

荒凉一梦 提交于 2019-11-28 23:37:12
I'm trying to include data files in distutils for my package and then refer to them using relative paths (following http://docs.python.org/distutils/setupscript.html#distutils-additional-files ) My dir structure is: myproject/ mycode.py data/ file1.dat the code in mycode.py , which is actually a script in the package. It relies on accessing data/file1.dat , refer to it using that relative path. In setup.py , I have: setup( ... scripts = "myproject/mycode.py" data_files = [('data', 'myproject/data/file1.dat')] ) suppose the user now uses: python setup.py --prefix=/home/user/ Then mycode.py will

Simple data validation

一曲冷凌霜 提交于 2019-11-28 20:53:13
问题 I'm writing a python module that will contain some functions that will manipulate a mongodb database. How can I go about validating input data passed to that function before saving it in database? For example, lets say one of the function in module is createUser(user) which accepts a python dictionary as argument. This dictionary contains user information to save in the database. I want to create an automated validation routine which checks that the dictionary structure matches the database

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

£可爱£侵袭症+ 提交于 2019-11-28 18:19:27
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' object has no attribute '__path__') The __path__ variable has has the current dir . I am not sure how

pyspark import user defined module or .py files

房东的猫 提交于 2019-11-28 16:54:13
I built a python module and I want to import it in my pyspark application. My package directory structure is: wesam/ |-- data.py `-- __init__.py A simple import wesam at the top of my pyspark script leads to ImportError: No module named wesam . I also tried to zip it and ship it with my code with --py-files as recommended in this answer , with no luck. ./bin/spark-submit --py-files wesam.zip mycode.py I also added the file programmatically as suggested by this answer , but I got the same ImportError: No module named wesam error. .sc.addPyFile("wesam.zip") What am I missing here? It turned out

Python can find a module…and then it can't

丶灬走出姿态 提交于 2019-11-28 14:37:08
I am finally going full steam into Python, but for some reason I have an issue where Python can find a module in the interactive CLI and then it can't when I write a script. The module is specifically mysql.connector located in /Library/Python/2.7/site-packages . As you can see from the interactive CLI session, it imports mysql.connector just fine. Echoing the sys.path shows 'Library/Python/2.7/site-packages' Here's a new CLI window (I'm on a Mac 10.10). Note: initially when I login I am popped into my home dir which of course is normal. wilkie:~ wilkie$ which python /usr/bin/python wilkie:~

A Python module and package loading confusion

允我心安 提交于 2019-11-28 12:44:26
Let's say I have something like this: . ├── run.py └── test ├── __init__.py ├── models │ ├── foo │ │ ├── baby.py │ │ └── __init__.py │ ├── __init__.py │ └── user.py └── start.py run.py from test import start start.py from .models import user user.py from . import foo print(foo.baby.Baby) baby.py Baby = "I am a baby" Now, when you run the run.py file... >>> python run.py ... traceback ... AttributeError: 'module' object has no attribute 'baby' But, if you change the start.py like this: from .models.foo import baby from .models import user everything works correctly. When the baby module in