简体   繁体   English

我的 Python 脚本在同一目录下找不到 JSON 文件

[英]My Python Script can't find a JSON file in the same directory

I am practicing some API work in python 3.7 using API star 0.5.X and my python script can't find the.json file that is in the same folder as the python file.我正在使用 API star 0.5.X 在 python 3.7 中练习一些 API 的作品,而我的 python 脚本找不到与 python 文件位于同一文件夹中的 .json 文件。 I am working on and running the script with Atom editor and I am working in a venv, which is fairly new to me.我正在使用 Atom 编辑器编写和运行脚本,我正在使用 venv,这对我来说是相当新的。

I am using a helper function to load in the JSON data using a "with open()" statement.我正在使用助手 function 使用“with open()”语句加载 JSON 数据。 I have tried using the relative and absolute file paths, and in both instances it is unable to locate the file.我尝试过使用相对和绝对文件路径,但在这两种情况下都无法找到文件。 I have tried launching the file in Atom using terminal and the MacOS finder.我尝试使用终端和 MacOS 查找器在 Atom 中启动文件。 This is what I have so far:这是我到目前为止所拥有的:

import json
from typing import List
import os
from apistar import App, Route, types, validators
from apistar.http import JSONResponse

print(os.getcwd())
os.chdir('/Users/{myusernamehere}/100days/apistar')
print(os.getcwd())

#helpers
def _load_employee_data():
   with open('employees.json') as f:
       employees = json.loads(f.read())
       return employees

the second print statement prints the correct file path, being the one that 'employees.json' and 'app.py' are located in.第二个打印语句打印出正确的文件路径,即“employees.json”和“app.py”所在的路径。

Since the problem is specific to your setup, it's hard to reproduce or provide a solution in code.由于问题特定于您的设置,因此很难在代码中重现或提供解决方案。 Your code itself looks to be fine, but there are two things that are likely to be the cause of your issues:您的代码本身看起来不错,但有两件事可能是导致您出现问题的原因:

  • When your script is running, Python needs access to the appropriate source folders and installed packages;当您的脚本运行时,Python 需要访问相应的源文件夹和已安装的包; you should let something like virtualenv manage this through a virtual environment.你应该让类似 virtualenv 的东西通过虚拟环境来管理它。 From the terminal, you can load the appropriate virtual environment with:从终端,您可以加载适当的虚拟环境:
    /path/to/your/venv/Scripts/activate.sh
    If you do, you should expect your script to find the same libraries it did during development in that virtual environment.如果您这样做,您应该期望您的脚本找到它在该虚拟环境中的开发过程中所做的相同的库。 Make sure you include something like a requirements.txt in your project to allow easy reinstalling of the same modules on a different machine, in a new virtual environment.确保在项目中包含诸如requirements.txt的内容,以便在新的虚拟环境中轻松地在不同机器上重新安装相同的模块。
  • Your script, when run by Python, has a 'working directory'.您的脚本在由 Python 运行时具有一个“工作目录”。 This is the directory that Python is started from and your script not being able to find the file (even though it may be in the same folder as the script itself) is probably due to Python being started from a different directory.这是 Python 启动的目录,您的脚本无法找到该文件(即使它可能与脚本本身位于同一文件夹中)可能是因为 Python 从不同的目录启动。

This was a problem due to how the Atom editor works.由于 Atom 编辑器的工作方式,这是一个问题。 It was solved by switching to vim.改用vim解决了。

I only partially understand but apparently this had something to do with Atom having a separate temp directory for working files, or something of that nature.我只是部分理解,但显然这与 Atom 有一个单独的临时目录来存放工作文件或类似的东西有关。 When using vim to edit the script, and then calling it in the terminal the problem was resolved.使用vim编辑脚本,然后在终端调用,问题解决。

Okay, So i had the same issue with i.c.w.好的,所以我对 i.c.w 也有同样的问题。 VScode:代码:

file = open('file.txt')
print(file.name)

resulted in FileNotFoundError导致FileNotFoundError

file.txt was 100% in the same folder... According to finder on my Mac, ánd the folder column in VS code! file.txt 100% 在同一个文件夹中……根据我 Mac 上的查找器,以及 VS 代码中的文件夹列!

i was pulling out my hair.我正在拔头发。 Switched a lot of interpreters, local python and Conda, to Python 3.8 instead of 3.9, back to python 2.8.切换了很多解释器,本地 python 和 Conda,到 Python 而不是 3.9,回到 python 2.8。 Nothing mattered.什么都不重要。

Till I changed: file = open('file.txt') to: file = open('file.txt', 'a') It didn't suddenly work, but I saw immediately in the "folder column" of VScode a new file.txt file popping up.直到我把: file = open('file.txt')改成: file = open file = open('file.txt', 'a')突然没用了,但是我马上在VScode的“文件夹栏”看到了一个弹出新的 file.txt 文件。 In an entirely different folder then where the pythonfile.py was located.在与 pythonfile.py 所在的完全不同的文件夹中。 So after that;那么在那之后; I pushed all local repo's to their remotes;我将所有本地回购推送到他们的遥控器; deleted the whole caboodle, and installed them one by one in a new folder through git clone.删除了整个caboodle,并通过git克隆将它们一个一个安装在新文件夹中。 I opened a new workspace added those project folders and since then it works like a charm.我打开了一个新的工作区,添加了那些项目文件夹,从那以后它就像一个魅力。

What exactly went wrong;究竟出了什么问题; im sorry, I have no idea.对不起,我不知道。 But for me, a fresh install of the repo's and VScode workspace is what did the trick.但对我来说,重新安装 repo 和 VScode 工作区才是关键所在。

I recently had the same error, on Visual Studio Code, I managed to solve it by instead of clicking the Run Python button, I used the terminal to cd into the project directory and run the python script like that, and no problems!我最近遇到了同样的错误,在 Visual Studio Code 上,我设法解决了它,而不是单击Run Python按钮,我使用终端 cd 进入项目目录并运行 python 脚本,没问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使它们在同一目录中也找不到 JSON 文件 - Can't find a JSON file even though they are in the same directory 由于目录错误,Python 找不到我的文件 - Python can't find my file because of a directory error 导入的模块找不到与 Python 脚本存在于同一目录中的输入文件? - Imported module can not find input file that exists in same directory as the Python script? 为什么 python 脚本找不到文件? - Why can't python script find file? Docker Python脚本找不到文件 - Docker Python script can't find file Python,在同一目录中找到一个文件 - Python, find a file in the same directory Python无法在添加到路径[Cygwin]的目录中找到脚本 - Python can't find script in directory added to path [Cygwin] Jupyter在同一目录中找不到python导入 - Jupyter can't find python imports in same directory 试图打开与我的 Python 脚本位于同一目录中的 csv 文件,但得到的错误 2 文件不存在? - Trying to open a csv file that lives in the same directory as my Python script, but getting error2 file doesn't exist? 如何使用脚本的相同路径在 VSCode 中运行 Python 脚本? (否则它找不到位于同一文件夹中的输入文件) - How to run a Python script in VSCode using the same path of the script? (otherwise it can't find an input file located in the same folder)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM