简体   繁体   English

从 PyCharm 中同一项目中的文件导入数据?

[英]Import data from files in same project in PyCharm?

This is a simple problem but I am struggling to solve it.这是一个简单的问题,但我正在努力解决它。 I have a project open in PyCharm Community Edition 2021.1.1.我在 PyCharm 社区版 2021.1.1 中打开了一个项目。 I have a.npy file with data, and it's located at src -> folder_name -> numpy_file.npy.我有一个包含数据的.npy 文件,它位于 src -> folder_name -> numpy_file.npy。 Then, I have a Python file under another directory in the same project, located at src -> folder_two -> python_code.py.然后,我在同一个项目的另一个目录下有一个 Python 文件,位于 src -> folder_two -> python_code.py。 In my Python file, I attempt to load the numpy file data using:在我的 Python 文件中,我尝试使用以下方法加载 numpy 文件数据:

np.load('folder_name/numpy_file.npy')

but it tells me there is no such file or directory.但它告诉我没有这样的文件或目录。

I have marked 'src' as my Sources Root.我已将“src”标记为我的 Sources Root。 What else am I missing?我还缺少什么? Thank you.谢谢你。

Easiest is to check your current path with import os and print(os.getcwd()) and navigate from there - you might be in a different folder than you think.最简单的方法是使用import osprint(os.getcwd())检查您的当前路径并从那里导航 - 您可能位于与您想象的不同的文件夹中。 As far as i know defining a sources folder does not mean that this is your current working directory.据我所知,定义源文件夹并不意味着这是您当前的工作目录。 You define such things in the run-configuration at Working directory .您可以在Working directory的运行配置中定义这些内容。 Also, with os you can change directories if that is what you want, eg os.chdir("..") to go up one folder etc.此外,使用os您可以根据需要更改目录,例如os.chdir("..")到 go 一个文件夹等。


Edit编辑

To further elaborate the problem - i tried reproducing your situation:为了进一步阐述问题 - 我尝试重现您的情况:

  1. Created a new pyCharm project in a folder called src (pure python, no venv, no welcome scripts)在名为src的文件夹中创建了一个新的 pyCharm 项目(纯 python,没有 venv,没有欢迎脚本)
  2. I created the subfolders folder_one and folder_two我创建了子文件夹folder_onefolder_two
  3. Inside folder_one i created a text-file with a sample-text called my_file.txtfolder_one我创建了一个文本文件,其中包含一个名为my_file.txt的示例文本
  4. Inside folder_two i created a python file called sub_module.py equivalent to your file.folder_two我创建了一个名为sub_module.py的 python 文件,与您的文件等效。
  5. I defined src as the sources root (which is not necessary i think): In PyCharm project tree > rightclick src folder > mark directory as > sources root我将src定义为源根目录(我认为这不是必需的):在 PyCharm 项目树中 > 右键单击 src 文件夹 > 将目录标记为 > 源根目录
  6. I created a run configuration:我创建了一个运行配置:
    • Script path: path to your sub_module.py脚本路径: sub_module.py的路径
    • Working directory: path to your src folder工作目录: src文件夹的路径
    • The rest can be empty rest 可以为空

Then finally, this is the content of my sub_module.py file:最后,这是我的 sub_module.py 文件的内容:

print("Im in folder: " + os.getcwd())

# Here i define which file i want to access - taking for granted we are in the src folder already
other_file = "folder_one" + os.sep + "my_file.txt"

# Here i check if the file really exists
if os.path.isfile(other_file):
    
    # Here i do some operations with the file and demonstrate os.path.abspath, which is not even needed
    print("I can access file " + os.path.abspath(other_file))
    with open(other_file) as f:
        print(f.read())

This returns me the content of my_file.txt .这将返回my_file.txt的内容。 I also uploaded the zipped PyCharm project here (where i do not know if it is fully legal).我还在这里上传了压缩的 PyCharm 项目(我不知道它是否完全合法)。 Hope this helps to relate.希望这有助于联系。

Did you try the entire path of the npy file?你试过npy文件的整个路径吗?

It all depends on the current directory where your Python program is executed.这完全取决于执行 Python 程序的当前目录。 If it starts from folder_two , then you should open the file by going "up" one level: '../folder_name/numpy_file.npy'如果它从folder_two开始,那么您应该通过“向上”一级打开文件: '../folder_name/numpy_file.npy'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM