怎么if __name__ == "__main__":办?
# Threading example
import time, thread
def myfunction(string, sleeptime, lock, *args):
while True:
lock.acquire()
time.sleep(sleeptime)
lock.release()
time.sleep(sleeptime)
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
Run Code Online (Sandbox Code Playgroud) 如何在完整路径下加载Python模块?请注意,该文件可以位于文件系统中的任何位置,因为它是一个配置选项.
想象一下这个目录结构:
app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
Run Code Online (Sandbox Code Playgroud)
我正在编码mod1,我需要从中导入一些东西mod2.我该怎么办?
我尝试了from ..sub2 import mod2但是我得到了"尝试非包装中的相对导入".
我google了一下,但发现只有" sys.path操纵"黑客.有没有干净的方式?
编辑:我__init__.py的所有人目前都是空的
EDIT2:我想这样做,因为SUB2包含了为子包(共享类sub1,subX等等).
Edit3:我正在寻找的行为与PEP 366中描述的相同(感谢John B)
我一直在为工作中的简单任务制作Python脚本,从来没有真正打扰包装它们以供其他人使用.现在我被分配为REST API创建一个Python包装器.我完全不知道如何开始,我需要帮助.
是)我有的:
(只是想尽可能具体)我已经准备好了virtualenv,它也在github中,python的.gitignore文件也是,还有用于与REST API交互的请求库.而已.
这是当前目录树
.
??? bin
? ??? /the usual stuff/
??? include
? ??? /the usual stuff/
??? lib
? ??? python2.7
? ??? /the usual stuff/
??? local
? ??? /the usual stuff/
??? README.md
27 directories, 280 files
Run Code Online (Sandbox Code Playgroud)
我甚至不知道把.py文件放到哪里,如果我做的话.
我想做什么:
使用"pip install ..."安装python模块
如果可能的话,我想要编写Python模块的一般步骤.
如何PYTHONPATH从Python脚本(或交互式shell)中找出系统变量中列出的目录?
我需要在我的脚本中直接从PyPi安装一个包.也许有一些模块或distutils(distribute,pip等)功能,它允许我只是执行像pypi.install('requests')和请求将被安装到我的virtualenv.
安装mechanize之后,我似乎无法导入它.
我尝试从pip,easy_install和via python setup.py install这个repo安装:https://github.com/abielr/mechanize.所有这一切都无济于事,因为每次我输入我的Python互动时,我得到:
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
>>>
Run Code Online (Sandbox Code Playgroud)
我之前运行的安装报告说它们已经成功完成,所以我希望导入能够正常工作.可能导致此错误的原因是什么?
使用python属性,我可以这样做
obj.y
Run Code Online (Sandbox Code Playgroud)
调用函数而不是仅返回值.
有没有办法用模块做到这一点?我有一个我想要的案例
module.y
Run Code Online (Sandbox Code Playgroud)
调用函数,而不是只返回存储在那里的值.
尝试导入OpenCV时,使用import cv2我得到以下错误:
/usr/local/lib/python2.7/dist-packages/cv2/__init__.py in <module>()
7
8 # make IDE's (PyCharm) autocompletion happy
----> 9 from .cv2 import *
10
11 # wildcard import above does not import "private" variables like __version__
ImportError: libSM.so.6: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
不确定如何解决这个问题 - 尝试使用谷歌新的Colaboratory工具.笔记本电脑在这里:https://drive.google.com/file/d/0B7-sJqBiyjCcRmFkMzl6cy1iN0k/view?usp=sharing
我今天在编码并发现了一些东西.如果我打开一个新的解释器会话(IDLE)并检查使用该dir函数定义的内容,我会得到:
$ 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', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', …Run Code Online (Sandbox Code Playgroud) python ×10
python-module ×10
python-2.7 ×2
idioms ×1
importerror ×1
mechanize ×1
namespaces ×1
opencv ×1
pip ×1
properties ×1
pypi ×1
python-3.x ×1
pythonpath ×1