0

I have a python project (in Pycharm), and I have, let's say, 2 folders in it. One is called data and the other is algorithms. The data folder has a python file where I import some data from an excel sheet. And another file where I have defined some constants.

The algorithm folder has, let's say, one python file where I import the constants and the data from the data folder. I use:

from data.constants import const
from data.struct import table 

When I run the algorithms (that are in the algorithm folder), things work perfectly. But when I change a constant in the constant file or the data in the excel sheet, nothing much changes. In other words, the constants are not updated when imported again and the same for the excel data imported. The old values of the constants and the table are used.

I tried to mark both folders as source root, but the problem persists.

What I do now, is close pycharm and reopen it again, but if there is a better way to handle this rather than closing and losing vars in the python console, I would be grateful to know about it!

jps
  • 20,041
  • 15
  • 75
  • 79
sos
  • 305
  • 1
  • 9

2 Answers2

0

I am not sure if I get it correct or not but try following. Once you change the constants in constants file try to import it again, i.e. do the following from data.constants import const.

After this you see that constants are not changed ?

  1. Please try this:
from constants.constant import v
print('v =', v)
del v
  1. The problem can be connected with cache. Here is similar problem as yours but for spider Spyder doesn't detect changes in imported python files

  2. Check this post pycharm not updating with environment variables

As it suggests you may have to take few steps. Set Environment Variables, or check the solution suggested here Pycharm: set environment variable for run manage.py Task .

armstar
  • 39
  • 3
  • my algorithm file starts with what you wrote. So every time I want to run it , that line is executed. But the constants do not change – sos Jul 08 '20 at 10:44
  • Do you use virtual env ? Please try: ´´´ import os print (os.getenv('PYTHONPATH')) ´´´ and see if it is empty or not ? – armstar Jul 08 '20 at 11:05
  • you mean the project interpreter? yes it is virtual environment – sos Jul 08 '20 at 11:09
  • I see, it is difficult to tell why it does not work. However I can check it in my system if you provide with a toy sample NOT working in your system ... – armstar Jul 08 '20 at 11:33
  • create a project in Pycharm >select that project and add a folder and name it constants> add a new python file there and name it constant > write this line line in it: `v = 10` > select the same project and add a folder and name it as algorithms> add a new python file in that folder and name it test and write inside the following: `from constants.constant import v
    print(v)`
    – sos Jul 08 '20 at 13:12
  • and then try changing the variable v and see if running the test file gives the new value or not – sos Jul 08 '20 at 13:14
  • I tried the example you described and it worked perfectly fine. Unfortunately I could not generate your scenario to come up with solution. However, I will post below an answer that I hope can help to understand what is happening, or bring closer to the solution. – armstar Jul 08 '20 at 15:23
  • Thanks a lot for your help. I had the feeling that it worked for me just fine in another project... What now works for me is the importlib.reload : `import constants.constant as ct`> `import importlib` >> `imporlib.reload(ct)` >>`print (ct.v)` works fine for me. But for the environment variables, I have added one env var for python and added it to the system vars but the problem is still there (of course when not using the importlib.reload option). So if you have an idea about which env var I have to add i would take it! – sos Jul 09 '20 at 07:33
0

I found the answer in this post : https://stackoverflow.com/a/5399339/13890967

Basically add these two lines into the settings>Console>Python Console

%load_ext autoreload
%autoreload 2

see this answer as well for better visualization: https://stackoverflow.com/a/60099037/13890967

and this answer for syntax errors: https://stackoverflow.com/a/32994117/13890967

sos
  • 305
  • 1
  • 9