在 python 中导入模块时遇到一些问题。这是我的文件夹结构
my_app/
app.py
__init__.py (I want to import a function from this file)
folder1/
__init.py
method1.py
folder2/
__init__.py
method.py
Run Code Online (Sandbox Code Playgroud)
在我的根中,__init__.py我有这个功能
def want_to_be_run_elsewhere():
pass
Run Code Online (Sandbox Code Playgroud)
在我的 app.py 中,我想导入这个函数并在我启动我的应用程序时运行它,但我不确定如何去做。
from my_app import want_to_be_run_elsewhere
Run Code Online (Sandbox Code Playgroud)
这会抛出一个名为 my_app 的无模块
据我所知,我拥有所有必要的__init__.py文件,所以它可能与 sys.path 相关?
我在这里阅读了一些类似的主题,但我无法解决这个问题。
我正在尝试导入实用程序文件,但只有当我通过脚本运行代码时才会遇到奇怪的错误.
当我运行test.py时
location:/home/amourav/Python/proj/test.py
码:
import os
os.chdir(r'/home/amourav/Python/')
print os.listdir(os.getcwd())
print os.getcwd()
from UTILS import *
Run Code Online (Sandbox Code Playgroud)
输出是:
['UTILS_local.py','UTILS.py','proj','UTILS.pyc']
/家庭/ amourav/Python的
回溯(最近一次调用最后一次):文件"UNET_2D_AUG17.py",第11行,来自UTILS import*ImportError:没有名为UTILS的模块
但是当我通过bash终端运行代码时,它似乎工作正常
bash-4.1$ python
>>> import os
>>> os.chdir(r'/home/amourav/Python/')
>>> print os.listdir(os.getcwd())
Run Code Online (Sandbox Code Playgroud)
['UTILS_local.py','UTILS.py','proj','UTILS.pyc']
>>> from UTILS import *
Run Code Online (Sandbox Code Playgroud)
等等等等 - 等等等等
我在Linux机器上运行Python 2.7.10
许多Python模块在不定义类的情况下保留内部状态,例如logging通过维护可访问的几个记录器getLogger().
你如何测试这样的模块?
使用标准unittest工具,我希望类中的各种测试TestCase重新导入我的测试模块,以便每次丢失其上下文.可以这样做吗?
我已经完成了虚拟python环境.当我尝试安装setuptools时,我得到以下内容:
python setup.py install --prefix=/home/dgomez/
Run Code Online (Sandbox Code Playgroud)
错误:
TEST FAILED: /home/dgomez//lib/python2.7/site-packages/ does NOT support .pth files
error: bad install directory or PYTHONPATH
You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from. The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/home/dgomez//lib/python2.7/site-packages/
and your PYTHONPATH environment variable currently contains:
''
Run Code Online (Sandbox Code Playgroud)
当我检查系统路径时,我收到以下输出:
>>> import sys
>>> import sys
/usr/lib/python27.zip
/usr/lib64/python2.7
/usr/lib64/python2.7/plat-linux2
/usr/lib64/python2.7/lib-tk
/usr/lib64/python2.7/lib-old
/usr/lib64/python2.7/lib-dynload …Run Code Online (Sandbox Code Playgroud) 我有一个调用API的脚本.为了加快脚本速度,我试图实现线程化.
当我在IDLE时,下面的脚本工作,但是当我尝试从命令行使用sys argv运行它时,我收到了下面列出的两种类型的错误.
错误1
Fatal Python error: PyImport_GetModuleDict: no module dictionary!
This application has requests the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
Run Code Online (Sandbox Code Playgroud)
错误2
Exception in thread Thread-1 (most likely raised during iterpreter shutdown):
Exception in thread Thread-2 (most likely raised during iterpreter shutdown):
Exception in thread Thread-3 (most likely raised during iterpreter shutdown):
Exception in thread Thread-5 (most likely raised during iterpreter shutdown):
Run Code Online (Sandbox Code Playgroud)
我找不到任何关于这些错误的信息.所以,任何帮助表示赞赏.下面是处理线程的脚本部分.
import threading
import diffbot
urls …Run Code Online (Sandbox Code Playgroud) python error-handling multithreading python-module python-multithreading
这个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)
)
Run Code Online (Sandbox Code Playgroud)
没有预期的效果.我想要它生产单一的myext.so,它做的; 但是当我通过它调用它时
python -m myext.so
Run Code Online (Sandbox Code Playgroud)
我明白了:
ValueError: Attempted relative import in non-package
Run Code Online (Sandbox Code Playgroud)
由于myext试图参考的事实.algorithms.
知道如何让这个工作吗?
我使用以下代码:
import paramiko
def runSshCmd(hostname, username, password, cmd, timeout=None):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, password=password,
allow_agent=False, look_for_keys=False, timeout=timeout)
stdin, stdout, stderr = client.exec_command(cmd)
stdin.flush()
data = stdout.read()
print (data)
client.close()
runSshCmd("10.128.12.32", "root", "C0mput3Gr!d", "ts_menu")
Run Code Online (Sandbox Code Playgroud)
当谈到stdout.read()时,它会挂起......有时它会在很长时间后打印输出.
你能否建议如果能解决这个问题?
我看到这个问题已经报道:
https://bugs.python.org/issue24026
在python中是否有更好的模块用于ssh连接和运行命令?
有没有办法让 protoc__init__.py在它用来生成 .py 文件的目录中生成空文件,以便它们可以用作模块?
python python-module python-2.x python-import protobuf-python
我有一个django应用程序,带有一个名为"app"的
模块现在这个模块有一个名为"urls.py"的文件,它有一个名为"HOME_URL " 的变量
我想做什么?
app_label = "app"
url = __import__(app_label).urls.HOME_URL
print url
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,但我希望你得到我正在尝试做的事情,如果没有请评论我将编辑问题以包含更多信息.
我最近决定学习通用算法,我需要安装Tensorflow软件包.Tensorflow仅在python 64位上运行,因此我在不卸载python 32位的情况下安装python 3.5.0 64位.因为我害怕在卸载python 32 bit时丢失我的软件包.问题是如何强制pip install在我的python 64位版本而不是32位版本上安装软件包.
python ×10
python-module ×10
import ×2
32bit-64bit ×1
cython ×1
distutils ×1
django ×1
module ×1
paramiko ×1
pip ×1
python-2.7 ×1
python-2.x ×1
python-3.x ×1
setuptools ×1
tensorflow ×1
unit-testing ×1