python-module

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

谁说我不能喝 提交于 2019-11-26 06:05:59
问题 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\', \

Collapse multiple submodules to one Cython extension

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 05:38:45
问题 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:

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

≯℡__Kan透↙ 提交于 2019-11-26 03:29:57
问题 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

Can modules have properties the same way that objects can?

别来无恙 提交于 2019-11-26 03:08:04
问题 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. 回答1: 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

Unable to import a module that is definitely installed

落花浮王杯 提交于 2019-11-26 01:47:03
问题 After installing mechanize, I don\'t seem to be able to import it. I have tried installing from pip, easy_install, and via python setup.py install from this repo: https://github.com/abielr/mechanize. All of this to no avail, as each time I enter my Python interactive I get: Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type \"help\", \"copyright\", \"credits\" or \"license\" for more information. >>> import mechanize Traceback (most recent call last): File \"<stdin>\",

Installing python module within code

爷,独闯天下 提交于 2019-11-25 23:59:26
问题 I need to install a package from PyPi straight within my script. Maybe there\'s some module or distutils ( distribute , pip etc.) feature which allows me to just execute something like pypi.install(\'requests\') and requests will be installed into my virtualenv. 回答1: You can also use something like: import pip def install(package): if hasattr(pip, 'main'): pip.main(['install', package]) else: pip._internal.main(['install', package]) # Example if __name__ == '__main__': install('argh') 回答2:

How to import a module given the full path?

吃可爱长大的小学妹 提交于 2019-11-25 22:53:28
问题 How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option. 回答1: For Python 3.5+ use: import importlib.util spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) foo.MyClass() For Python 3.3 and 3.4 use: from importlib.machinery import SourceFileLoader foo = SourceFileLoader("module.name", "/path/to/file.py").load

How to do relative imports in Python?

↘锁芯ラ 提交于 2019-11-25 22:15:20
问题 Imagine this directory structure: app/ __init__.py sub1/ __init__.py mod1.py sub2/ __init__.py mod2.py I\'m coding mod1 , and I need to import something from mod2 . How should I do it? I tried from ..sub2 import mod2 but I\'m getting an \"Attempted relative import in non-package\". I googled around but found only \" sys.path manipulation\" hacks. Isn\'t there a clean way? Edit: all my __init__.py \'s are currently empty Edit2: I\'m trying to do this because sub2 contains classes that are

Unable to import a module that is definitely installed

孤街浪徒 提交于 2019-11-25 21:35:36
After installing mechanize , I don't seem to be able to import it. I have tried installing from pip, easy_install, and via python setup.py install from this repo: https://github.com/abielr/mechanize . All of this to no avail, as each time I enter my Python interactive I get: Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import mechanize Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named mechanize >>> The installations I ran previously reported that

Importing installed package from script raises “AttributeError: module has no attribute” or “ImportError: cannot import name”

喜夏-厌秋 提交于 2019-11-25 21:34:21
问题 I have a script named requests.py that imports the requests package. The script either can\'t access attributes from the package, or can\'t import them. Why isn\'t this working and how do I fix it? The following code raises an AttributeError . import requests res = requests.get(\'http://www.google.ca\') print(res) Traceback (most recent call last): File \"/Users/me/dev/rough/requests.py\", line 1, in <module> import requests File \"/Users/me/dev/rough/requests.py\", line 3, in <module>