我正在尝试在 python 中导入文件
我的目录结构是
maindir
__init__.py
constants.py
subdirectory
__init__.py
code.py
Run Code Online (Sandbox Code Playgroud)
我正在尝试在代码文件中导入常量
import sys
sys.path.append('/home/ching/maindir')
from maindir import constants
Run Code Online (Sandbox Code Playgroud)
即使在那之后我也收到这个错误
ImportError: No module named maindir
Run Code Online (Sandbox Code Playgroud)
有人知道出了什么问题吗?
对 python 来说相对较新,我在工作中使用它来实现实验室设备自动化。
我有一个脚本位于目录 B 中。目录 B 的父目录是目录 A。目录 A 包含另一个我需要输入和导入模块的目录。结构如下:
A
B C
myscript.py
E F
m1.py m2.py
Run Code Online (Sandbox Code Playgroud)
我需要将 m1.py 和 m2.py 导入到我的脚本中。我无法将上面的脚本移动到 A 目录中,因为它正在访问我编写并转储到 B 中的一堆其他模块。出于公司特定原因,我需要维护目录 C 及其下的所有内容的目录结构(不要获取我开始了。)
我的问题是,如何智能地导入这些?我不想对它们的路径进行硬编码,因为它们将存在于 SVN 中并且根可能会改变。我正在考虑使用循环来扫描?我只是不熟悉使这成为可能的语法。欢迎所有解决方案!
我对 Python 很陌生,正在尝试使用 Python 在 Raspberry Pi2 中制作一个小项目
目前我有 2 个代码文件run1.py和run2.py
我想写一个if-else条件Project.py,但我不知道如何正确编写代码......
if (condition is true) ----> run the code from file "run1.py"
else ----> run the code from file "run2.py"
Run Code Online (Sandbox Code Playgroud)
__main__是关于“ ”或“ ”的主题import os吗?我也想了解它是如何工作的,但还没有真正理解。
谢谢
就网络应用程序上的最少运行时间而言,将我的导入保存在views.py中的理想位置是什么
假设我想使用外部模块验证和处理一些表单条目。我当前的代码:
from django.shortcuts import render
from .forms import *
import re
import module1
import module2
def index(request):
form = MyForm()
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
#
#Process my stuff here using re, module1, module2
#
return render(request, 'index.html', context)
Run Code Online (Sandbox Code Playgroud)
但是,如果条件失败,提前导入模块 re、模块 1、模块 2 有什么用呢if form.is_valid():?还是条件if request.method == 'POST':不合格?那是表格从未提交的时候。
在这些条件通过后导入这些模块(因为那是实际需要它们的时候)会在这些条件失败时导致程序或 Web 应用程序的运行时开销减少吗?在不需要时避免不必要的进口?
我的想法的伪代码:
if form.is_valid():
import re
#Perform some regex matches and stuff
if (above re matches succeed):
import module1
#Process my …Run Code Online (Sandbox Code Playgroud) 我尝试按照此处的说明使用 Boost.Python。源代码位于该网页中。我可以编译、链接这个简单的示例代码,但无法在 python 命令行中导入生成的模块。总是报错:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named hello_ext
Run Code Online (Sandbox Code Playgroud)
我不知道出了什么问题,因为该页面只是说:“就是这样。我们完成了。我们现在可以将其构建为共享库。生成的 DLL 现在对 Python 可见。” 这是我的构建环境:
那么,你能告诉我如何在 python 中导入模块吗?多谢。
我一直在尝试从一篇使用 Kinect Fusion 库的论文中重现一个框架。
当我运行测试脚本时,出现错误:
(darnn) weizhang@long:~/DA-RNN$ sudo ./experiments/scripts/test_kinect_fusion.sh 0
+ set -e
+ export PYTHONUNBUFFERED=True
+ PYTHONUNBUFFERED=True
+ export CUDA_VISIBLE_DEVICES=0
+ CUDA_VISIBLE_DEVICES=0
++ date +%Y-%m-%d_%H-%M-%S
+ LOG=experiments/logs/test_kinect_fusion.txt.2018-06-17_12-44-13
+ exec
++ tee -a experiments/logs/test_kinect_fusion.txt.2018-06-17_12-44-13
+ echo Logging output to experiments/logs/test_kinect_fusion.txt.2018-06-17_12-44-13
Logging output to experiments/logs/test_kinect_fusion.txt.2018-06-17_12-44-13
+ ./tools/test_kinect_fusion.py --gpu 0 --imdb rgbd_scene_trainval --cfg experiments/cfgs/rgbd_scene.yml --rig data/RGBDScene/camera.json
Traceback (most recent call last):
File "./tools/test_kinect_fusion.py", line 15, in <module>
from kinect_fusion import kfusion
ImportError: libkfusion.so: cannot open shared object file: No such file or …Run Code Online (Sandbox Code Playgroud) 你好,我想在我的 python 副本中为自己实现一个小的库访问控制,我想知道是否有某种方法可以在导入系统上创建一个钩子,这样我的程序可以在每次导入时检查一个文件,如果该程序有权导入该库。我看到了一些有关 sys 库和 pep 302 ( https://www.python.org/dev/peps/pep-0302/ ) 的信息,但我不明白。
导入 sklearn 数据集时,例如。
from sklearn.datasets import fetch_mldata
from sklearn.datasets import fetch_openml
Run Code Online (Sandbox Code Playgroud)
我收到错误
Traceback (most recent call last):
File "numbers.py", line 1, in <module>
from sklearn.datasets import fetch_openml
File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/__init__.py", line 64, in <module>
from .base import clone
File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/base.py", line 11, in <module>
import numpy as np
File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/core/__init__.py", line 93, in <module>
from . import numerictypes as nt
File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/core/numerictypes.py", line 86, in <module>
import numbers
File "/Users/airocoop/repos/Numbers/numbers.py", line …Run Code Online (Sandbox Code Playgroud) 我刚刚开始尝试学习使用硒。当我尝试从 selenium 导入模块 webdriver 时,如下所示:
from selenium import webdriver
Run Code Online (Sandbox Code Playgroud)
只有这个,但外壳说
ImportError: cannot import name 'webdriver' from partially initialized module 'selenium' (most likely due to a circular import)
Run Code Online (Sandbox Code Playgroud)
我该怎么办?谢谢
假设我编写了一个名为的 python 模块my_module:
# my_module.py
import numpy as np
def my_function():
print("Hello, module!")
Run Code Online (Sandbox Code Playgroud)
然后我my_module使用内置函数导入并检查它dir():
>>> import my_module
>>> dir(my_module)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'my_function', 'np']
Run Code Online (Sandbox Code Playgroud)
np显示在由 定义的名称列表中my_module。这很烦人,因为我经常想dir()通过my_module. 但是在上面的结果中,我不清楚如何判断它np是本地的my_module还是只是my_module导入的某个包,这(在我看来)混淆了my_module.
所以我的问题是,我如何构建my_module以便只有它定义的函数/类/等出现dir(my_module)?或者等效地,这样dir(my_module)就不会打印出导入的每个模块my_module.py?
我想:
这是可能的,因为dir(other popular packages)通常不会列出他们的每个导入:
>>> import scipy
>>> print([x for x in dir(scipy) if …Run Code Online (Sandbox Code Playgroud)python-import ×10
python ×9
import ×3
importerror ×2
boost ×1
bots ×1
c++ ×1
django ×1
dll ×1
file ×1
hook ×1
if-statement ×1
kinect ×1
library-path ×1
numpy ×1
package ×1
python-3.5 ×1
python-3.x ×1
raspberry-pi ×1
scikit-learn ×1
selenium ×1
structure ×1