1

I would like to ask for some help, I'm not able to force this to work I have working app, I starting for b23.py or b55.py or any other xx.py file from Start folder Inside of the file I have some variables etc. Main part runs in app.py & until now I was hardcoded var inside app.py but now I want to use value from b23.py or b55.py I know how to import from Data\worksok.py "from Data.kaso import *" - works fine

But how to import from Start folder, directly from bxx.py file?

Structure:
Start (main dir)
    |-b23.py
    |-b55.py
    |-b(xx number).py
    |-Data
     |-__init__.py
         |-kaso.py
         |-classes
                 |-__init__.py
                 |-app.py

I was trying some solution from here Importing from a relative path in Python or here Import a module from a relative path but somehow this doesn't work

I'm pretty sure I'm doing smth wrong but I'm just too weak for py for now:/

I will appreciate any advice)

buran
  • 13,682
  • 10
  • 36
  • 61
khole02
  • 11
  • 1
  • From which file do you want to do the import? If those are intended to be part of the module, then they need to be inside the `Data` directory. You can't do a relative import from outside the module. – Tim Roberts Feb 21 '23 at 19:41
  • I want to import or pass some value from any bXX.py file (my starting file from Start folder) & use it in app.py (classes), untill now I just loading value in app.py via "open(Path(__main__.__file__).stem+'.v4') as f:" every time, but i want to change this & keep all config in bXX.py – khole02 Feb 21 '23 at 19:46
  • So, `b23.py` and `b55.py` are main programs? If so, then the right thing to do is to have function in the modules that you call to pass in the values. Make the main program be in control, and the modules are slaves. PUSH the right values, don't have the module PULL them. – Tim Roberts Feb 22 '23 at 02:05
  • I think `app.py` is main program and `b23.py` and `b55.py` have variables, functions etc. that are needed in `app.py`. – iamdeedz Feb 22 '23 at 16:24

1 Answers1

0

If you put your main file (Twitch.py) in the top directory (TwitchChannelPointsMiner folder) and have your data files in the data folder which itself is in the TwitchChannelPointsMiner folder, then you can do this in your Twitch.py. from data.example import *. You can change example to whatever your file is called and have one of these imports for each data file.

iamdeedz
  • 109
  • 8
  • unfortunately doesn't work:/ ModuleNotFoundError: No module named 'start' – khole02 Feb 21 '23 at 21:07
  • what is the code you ran? – iamdeedz Feb 21 '23 at 21:11
  • Im trying to rebuil Twitch-Channel-Points-Miner-v2, you have there example.py with some variables. I want add more & use directly in TwitchChannelPointsMiner\classes\Twitch.py, this runs python3 – khole02 Feb 21 '23 at 21:22
  • You should just be able to do `from start.example import *` at the start of Twitch.py and use all your variables. – iamdeedz Feb 21 '23 at 22:18
  • Oh wait. I just ran the program from command prompt instead of pycharm and got an error. Sorry! I have things to do right now unfortunately but will get back to you tomorrow. Sorry for this. I also can't edit my answer due to the edit queue being full. – iamdeedz Feb 21 '23 at 22:42
  • If you put your main file (`Twitch.py`) in the top directory (`TwitchChannelPointsMiner` folder) and have your data files in the `data` folder which itself is in the `TwitchChannelPointsMiner` folder, then you can do this in your `Twitch.py`. `from data.example import *`. You can change `example` to whatever your file is called and have one of these imports for each data file. I still can't edit my answer for some reason. As soon as I am able to, I will. – iamdeedz Feb 22 '23 at 16:40