如何拼魔方的最底层
如何拼魔方的最底层
最满意答案
如果你希望能够做到 -
from utilities.utility import UtilityFunctionA
- 从function.py
,当从顶级目录运行时 -.
。 您需要使用-m
选项将python文件作为模块运行。 示例 -python3 -m src.functions.function
但是上面的方法只有在你总是运行时才有效
.
目录(顶级目录)。 我总是手动将顶级目录添加到PYTHONPATH
环境变量中。或者使用
os.path
和相对路径使用__file__
以编程方式将其添加到sys.path
。 示例 -import sys import os.path path_to_top = os.path.abspath(os.path.join(os.path.dirname(__file__),'..','..')) sys.path.append(path_to_top)
在此之后进行导入。 如果目录结构总是保持不变,那么即使在其他系统中也是如此,而不必设置任何环境变量。
根据更新的要求 -
现在我收到OSError:File b'utilities / data.csv'不存在的错误
有没有办法设置我的模块从PyCharm和命令行运行?
对于读取文件,Python使用当前工作目录作为起点,并且相对于当前工作目录解析所有相对路径。 依赖当前工作目录作为特定目录绝不是一个好主意,因为我们可以使用脚本的绝对路径从任何地方运行python脚本。 对于加载文件,我们可以使用
os.path
和相对路径来创建文件的绝对路径。 示例 -import os.path path_to_datacsv = os.path.abspath(os.path.join(os.path.dirname(__file__),'..','..','utilities,'data.csv'))
If you want to be able to do -
from utilities.utility import UtilityFunctionA
- from withinfunction.py
, when running from the top level directory -.
. You need to run the python file as a module , using-m
option. Example -python3 -m src.functions.function
But The above method only works if you always run from the
.
directory (top-level directory) . I always either add the top-level directory manually into thePYTHONPATH
environment variable.Or use
os.path
and relative path to add it tosys.path
programmatically using__file__
. Example -import sys import os.path path_to_top = os.path.abspath(os.path.join(os.path.dirname(__file__),'..','..')) sys.path.append(path_to_top)
After this do the import. If the directory structures would always remain the same, this would work even in other systems, without having to set any environment variable.
As per the updated requirements -
Now I receive the error that OSError: File b'utilities/data.csv' does not exist
Is there any way to setup my module to run both from within PyCharm and also from the command line?
For reading files, Python uses the current working directory as the start point , and all relative paths are resolved relative to the current working directory. It is never a good idea to rely on the current working directory to be a particular directory as we can run python scripts from anywhere using absolute path to the script. For loading files as well, we can use
os.path
and relative paths to create the absolute path to the file. Example -import os.path path_to_datacsv = os.path.abspath(os.path.join(os.path.dirname(__file__),'..','..','utilities,'data.csv'))
相关问答
更多-
PYTHON:nosetests用多个模块/文件导入文件路径(PYTHON: nosetests import file path with multiple modules/files)[2019-10-28]
一切都需要与您的执行点相关。 你正在使用ex48的根目录运行你的鼻子命令,因此你所有的导入都需要关于那个位置。 因此,在game.py您应该针对ex48导入。 因此: from ex48.otherpy import House 应该将相同的逻辑应用于引用temp文件夹的示例。 from ex48.temp import temp The only solution I have found is to post the following to the top of the main file: tr ... -
从相关目录导入用户创建的Python模块和数据文件(Importing user-created Python modules and data files from relative directories)[2022-09-10]
如果你希望能够做到 - from utilities.utility import UtilityFunctionA - 从function.py ,当从顶级目录运行时 - . 。 您需要使用-m选项将python文件作为模块运行。 示例 - python3 -m src.functions.function 但是上面的方法只有在你总是运行时才有效. 目录(顶级目录)。 我总是手动将顶级目录添加到PYTHONPATH环境变量中。 或者使用os.path和相对路径使用__file__以编程方式将其添加到sy ... -
Python模块的绝对和相对导入:matplotlib示例(Absolute and relative import of Python modules: a matplotlib example)[2023-05-06]
关于matplotlib代码库的一个重要的事情是,它非常古老(git历史可以追溯到2003年并且已经失去了几年),大型(大于93k的python线,17k行的c ++),并且有超过450个贡献者。 看看git-blame(在2.x分支之外但是导入相当稳定)显示: 08:29 $ git blame matplotlib/artist.py | head -n 18 5fca7e31 (Thomas A Caswell 2013-09-25 11:36:00 -0500 1) fro ... -
您知道config.py所在目录中文件的正确相对路径 您知道config.py所在目录的正确相对路径(在您的情况下, .. ) 这两件事都与系统无关,除非您更改项目的结构,否则不会更改。 只需使用os.path.join('..', config.path_repative_to_config)将它们添加到一起 You know the correct relative path to the file from the directory where config.py is located You kn ...
-
from rnaspace.dao.storage_configuration_reader import storage_configuration_reader 这是错误的,因为“dao”目录中没有“storage_configuration_reader”目录 这应该是这样的: from rnaspace.dao import storage_configuration_reader 编辑: 或者这样: import rnaspace.dao.storage_configuration_reade ...
-
您使用模块的每个文件都必须导入该模块。 每个模块都有自己的命名空间。 您在该文件名中明确导入的内容在该命名空间中可用。 因此,如果在两个文件中都需要os ,则应在两个文件中导入它们。 Each file that you are using a module in, must import that module. Each module is its own namespace. Things you explicitly import within that file are available in ...
-
使用显式相对导入: from . import subapi Use an explicit relative import: from . import subapi
-
假设您拥有相对于调用脚本的位置的python项目目录,并且您还有要创建文件夹的绝对目录,或者它相对于您调用脚本的位置,请参阅以下内容: import os rel_directory=os.path.relpath(target_directory,base_directory) 这里第二行返回从base_directory到target_directory的相对路径。 如果你想要当前的工作目录,你可以做一些像os.path.relpath('.','/')或os.path.realpath('.')或 ...
-
数据文件的python搜索路径(非模块) - 文本文件(* txt)(python search path for data files (non-modules) - text files (*txt))[2023-04-16]
没有这样的选择。 如果文件名中未指定路径,则仅搜索当前工作目录。 There is no such option. Only the current working directory is searched if no path is specified in the filename. -
Python - 相对导入(Python - Relative import)[2022-06-21]
问题是因为Foo没有__init__.py ,因此它不被视为包。 相对导入中的每个句点都指包。 当你这样做 from ..src.output import * 在bar.py ,第一个句点指的是当前包, modules 。 第二个时期是指上面的包, Foo 。 由于Foo实际上不是一个包,因此会出错。 PS Wildcard的进口令人不悦。 特别是当您从这样一个远程相关的包导入时,我会尝试将其重构为显式导入。 The problem is because Foo has no __init__.py, ...