2

My PyCharm project contains folders such as src, data, notebooks etc.

Within my notebooks folder, I have a small notebooks.py file which contains:

import sys
sys.path.append('../src')

So within my notebooks, I do the following:

import notebooks
import preprocessing.some_module

However, all my code is naturally within src and is full of imports such as:

from src.preprocessing.some_module import SomeClass

So when I try to import something within the notebooks, I get errors telling me src is not found.

If I set the content root at src within the Project Structure, all the other folders simply disappear so it's not really a solution.

So, is there a way for me to be able to refer to my project modules simply as preprocessing.some_module rather than src.preprocessing.some_module so that I can run my code smoothly both using PyCharm and on AWS?

Also, I have a bunch of constants in my code such as:

SOME_FOLDER = '../../data/some_folder'

These work fine for now, while I run from PyCharm but I fear this relativity might also get messed up when I start running the code from Jupyter. What is the best way to define these paths so they work no matter what?

rudolfovic
  • 3,163
  • 2
  • 14
  • 38
  • 1
    Depends on how you configure your project layout, if using [`setup.py` see this answer](https://stackoverflow.com/q/62498127) or you can just mark `src` as [sources root](https://stackoverflow.com/a/21241988) - this is the PyCharm canonical. I think the jupyter-nootebook canonical `src` question is [this one](https://stackoverflow.com/q/53049195). – bad_coder Feb 15 '22 at 16:08
  • Also see [this answer](https://stackoverflow.com/a/31575455). – bad_coder Feb 15 '22 at 16:11
  • @bad_coder thanks, the sources root trick resolved the first problem :) what about making path strings (in the resource folders) usable no matter which module they are called from? i.e. how can I somehow pin them to the source of the "project dir"? – rudolfovic Feb 15 '22 at 16:40
  • Normally when you're linking to something below the `src` folder that part is meant not to be installed (the base package inside `src` is what you'll be installing), if it's for something that's generated locally and later uploaded as a separate folder (like for example documentation or tests). So since the paths are relative to your development environment and not the installed package it's not an issue, as for relative paths to data folders that aren't importable see [this answer](https://stackoverflow.com/a/58941536) – bad_coder Feb 15 '22 at 16:50
  • 1
    Also consider marking resources [as resource folders](https://stackoverflow.com/q/32530558) – bad_coder Feb 15 '22 at 16:51
  • @bad_coder - I did (mark as resources) though I didn't quite get what that changes exactly :/ – rudolfovic Feb 15 '22 at 16:53
  • See [Configuring Project Structure](https://www.jetbrains.com/help/pycharm/configuring-project-structure.html) *"resource roots the Resources root icon (PyCharm Professional only) are intended for resource files in your application (images, Style Sheets, and so on). By assigning a folder to this category, you tell PyCharm that files in it and in its subfolders can be referenced relative to this folder instead of specifying full paths to them."* – bad_coder Feb 15 '22 at 17:02
  • There's probably an integration (somewhere?) of [jupyter-notebook config file](https://jupyter-notebook.readthedocs.io/en/stable/config.html) for [PyCharm](https://www.jetbrains.com/help/pycharm/jupyter-notebook-support.html) but I don't have specific jupyter notebook knowledge past this point. (If you solve this it will make for a popular question...) – bad_coder Feb 15 '22 at 17:03

0 Answers0