python-import

Correct import and package structure now that __init__.py is optional

﹥>﹥吖頭↗ 提交于 2019-12-31 03:09:05
问题 I'm building a package that contains scripts to be run. They import modules contained in a subfolder directly under the script. Now that __init__ is not required after Python 3.3, what's the correct file structure and import statement to have? I'd like to not have to specify the import from the topmost folder down, only as a relative path, here sub/module . This is the current state of the file structure: Root\ src\ sub\ module.py script.py parent_module.py setup.py # Inside script.py import

can't import import datetime in script [duplicate]

血红的双手。 提交于 2019-12-30 19:55:25
问题 This question already has answers here : Importing installed package from script raises “AttributeError: module has no attribute” or “ImportError: cannot import name” (2 answers) Closed 10 months ago . I cannot import datetime from a python script, but I can from the terminal command line. 1)import datetime 2)From datetime import datetime month = datetime.datetime.now().strftime("%B") print month These lines of code work when entered one by one into the command line Any ideas? I'm running 2.7

can't import import datetime in script [duplicate]

谁都会走 提交于 2019-12-30 19:54:56
问题 This question already has answers here : Importing installed package from script raises “AttributeError: module has no attribute” or “ImportError: cannot import name” (2 answers) Closed 10 months ago . I cannot import datetime from a python script, but I can from the terminal command line. 1)import datetime 2)From datetime import datetime month = datetime.datetime.now().strftime("%B") print month These lines of code work when entered one by one into the command line Any ideas? I'm running 2.7

Python Shared Libraries: RTLD_GLOBAL segfault

心已入冬 提交于 2019-12-30 11:56:25
问题 I work with a python swig-wrapped C++ library. In it's __init__.py file, it sets the dlopen flag RTLD_GLOBAL before importing the shared object file containing the implementation code. This causes the subsequent import of scipy.linalg to segfault, at least on my machine. I think this behavior depends on the way in which scipy was built and what it's linked against though. # minimal example of what's going on $ cat test.py import sys import ctypes flags = sys.getdlopenflags() sys

Python Shared Libraries: RTLD_GLOBAL segfault

[亡魂溺海] 提交于 2019-12-30 11:56:13
问题 I work with a python swig-wrapped C++ library. In it's __init__.py file, it sets the dlopen flag RTLD_GLOBAL before importing the shared object file containing the implementation code. This causes the subsequent import of scipy.linalg to segfault, at least on my machine. I think this behavior depends on the way in which scipy was built and what it's linked against though. # minimal example of what's going on $ cat test.py import sys import ctypes flags = sys.getdlopenflags() sys

Are there standard aliases for modules in Python?

雨燕双飞 提交于 2019-12-30 10:25:39
问题 Following the guidelines proposed in this post, I am changing all the from module import function function(agt) by: import module as mdl mdl.function(agt) in my codes. I am trying to use commonly used aliases rather than personal ones. Is there a list of some kind on the internet summing-up all well-used aliases ? For instance, these appear to be pretty common: import numpy as np import math as m import matplotlib.pyplot as plt What about aliases for scipy.linalg , time , scipy.io , cmath and

What does “import” prefer - .pyd (.so) or .py?

柔情痞子 提交于 2019-12-30 08:28:45
问题 I have 2 files in same directory, a compiled library file and source file: . ├── a.py └── a.pyd It looks like import a that actually imports the a.pyd module. But I can't find some official document guaranteeing that. Does any one know about the import ordering of different file type? This same question applies to Unix Python extensions (.so) 回答1: In a typical Python installation, the ExtensionFileLoader class has precedence over the SourceFileLoader that is used for .py files. It's the

exe built with cx_Freeze, PyQt5, Python3 can't import ExtensionLoader_PyQt5_QtWidgets.py and run

邮差的信 提交于 2019-12-30 07:44:54
问题 I'm trying to pack my app Python3, PyQt5 for Windows using cx_Freeze. I've installed Python 3.4.3, Python-win32, PyQT5, cxfreeze. Application itself, run in console, works fine. I try to pack it with cx_freeze: python setup.py build_exe. It works on the same host. But when I move it to another clean installation WinXP it gives an error: Traceback: File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27 in <module> File "pyftp1.py" in 7, in <module> File "c:\python\32

module reimported if imported from different path

♀尐吖头ヾ 提交于 2019-12-30 04:36:07
问题 In a big application I am working, several people import same modules differently e.g. import x or from y import x the side effects of that is x is imported twice and may introduce very subtle bugs, if someone is relying on global attributes e.g. suppose I have a package mypakcage with three file mymodule.py, main.py and init .py mymodule.py contents l = [] class A(object): pass main.py contents def add(x): from mypackage import mymodule mymodule.l.append(x) print "updated list",mymodule.l

Flask - ImportError: No module named app

夙愿已清 提交于 2019-12-30 03:39:29
问题 First I created __init__.py from flask import Flask app = Flask(__name__) Then in a separate file, in the same directory, run.py from app import app app.run( debug = True ) When I try to run run.py , I get the error Traceback (most recent call last): File "run.py", line 1, in <module> from app import app ImportError: No module named app 回答1: __init__.py is imported using a directory. if you want to import it as app you should put __init__.py file in directory named app a better option is just