How to set the python searching directory of data file in another sub-folder in pycharm?
<p>Here my project has folder alignment as follows:</p>

<pre><code> myProject:
    ---------
     main.py
 -------------
     folder1:
         test.py
 --------------
     folder2:
         lib.py
         __init__.py
       ---------------
         folder_for_data:
              data_file1
              data_file2
</code></pre>

<p>So how to specify the searching path if I want to open the <code>data_file1</code> in the <code>test.py</code> scripts ? Actually, I can successfully open the data file in <code>main.py</code> by directly specifying the path as <code>folder2/folder_for_data/data_file1</code>. However, as I do the same thing in <code>test.py</code>, PyCharm tells me there is no such file or directory. I already add <code>myProject</code> to Content Root in pycharm, but why I can't open the file as I specified above. </p>

<p>By the way, I can import modules in lib.py with <code>from folder2.lib import module</code>. I guess if there are any tricks required to specify searching root path for data files?     </p>

<p>For the most part, this question is independent of PyCharm. It's mostly about how to import modules and load data files in Python using relative paths.</p>

<p>Importing modules is different than opening data files, for quite a number of reasons. It is affected by: the working directory that you run Python from, the PYTHONPATH, use of Python 2 vs. Python 3.4+, whether you have setup your work as a Python package, and whether you have an <strong>init</strong>.py in place.</p>

<p>For the data file, my suggestion: use Python's facilities to find the directory of the running script, then use the path modules to make a directory relative to that.</p>

python pycharm