2

When I am adding new functions to a file I can't import them neither if I just run the script in terminal or if I launch ipython and try importing a function there. I have no .pyc files. It looks as if there is some kind of caching going on. I never actually faced such an issue even though have been working with various projects for a while. Why could it happen?

What I see is the following: I launch ipython and the functions that were written long time ago by other programmers can be imported fine. If I comment them out and save the file, they still can be imported without any issues. If I write new functions they can't be imported.

The directory is git directory, I cloned the repo. Then the new branch was created and I switched to it. Python version is 3.7.5, and I am working with virtual environment that I created some time ago which I activated with source activate py37.

I don't know whether its important but I have an empty __init__.py in the folder where script is located.

The code (I don't think its relevant, but still):

import hail as hl
import os
      
class SeqrDataValidationError(Exception):
    pass

# Get public properties of a class
def public_class_props(cls):
    return {k: v for k, v  in cls.__dict__.items() if k[:1] != '_'}

def hello():
    print('hello')

public_class_props is an old function and can be imported, but hello - can't.

Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87

2 Answers2

0

assuming that you've successfully cloned the project into your machine, If it is a problem with importing functions or methods from a different .py file, please check the points below.

  1. check your working directory, whether you're in the same directory where the .py file/module with functions/methods exist.

  2. once you import any function/method from a module, even you comment out the function/method and save the .py file, it won't affect the already imported functions as long as you re-import it.

  3. as long as it is a problem of importing from your own .py files, the virtual environment has nothing to do with that.

EDITED:

check this link, it may give you some information about how caching works while importing python modules.

Venkatesh Dharavath
  • 500
  • 1
  • 5
  • 18
  • I relaunch ipython and rerun python, so its not 2. Other old functions are imported fine, so its not 1 either. – Nikita Vlasenko Dec 01 '20 at 17:44
  • __init__.py would be empty only. it just says that the directory is a python package, nothing to worry about it. if you could put some code, it would help us understand the situation better. – Venkatesh Dharavath Dec 01 '20 at 17:54
0

The issue was that PYTHONPATH was set to a wrong folder. We had two folders with the project: old and new, similarly named, with identical project structure (but different file contents) and PYTHONPATH was set to the old project.

Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87