Setting up an environment for testing modules locally

No, I don’t create a __init__.py for client_code and server_code.

Until yesterday I used two strategies:

  • A mess of if anvil.server.context.type == 'uplink' to decide how to import what
  • When that didn’t work, a mess of try - except
  • A mess of folders tagged as source code in PyCharm (I think it changes some environment variables before starting Python, not 100% sure)

The problem is that, once I get my local environment working, PyCharm does its best to figure out what’s going on, but sometimes it just fails to find the correct references. The problems are even bigger when I have dependencies and start tagging folders on other repositories as source folders. And the problems increase when some apps and some modules have the same name.


Right now I’m in the process of setting up the test environment for a little app that I have almost finished without doing any tests (yeah I know, not really test driven, but this is 50% UI, 50% dependencies and the remaining 0.0something% the new app itself). The dependencies don’t play nicely with each other, so I need to setup a test environment on my PC. Unfortunately (or rather luckily) I just got a new PC. I just installed Python, hopefully I will be ready to start working on the tests tomorrow.

Before the old PC gave up, I had just learned that the reason why the tests folder has trouble finding the modules is what Anvil does with the __init.py__:

__path__ = [__path__[0]+"/server_code", __path__[0]+"/client_code"]

The folder structure is flattened, so it’s easier to import from both server_code and client_code, but PyCharm doesn’t like it.

I tried adding:

__path__ = [__path__[0]+"/server_code", __path__[0]+"/client_code", __path__[0]+"/tests"]

and I got the tests to run, but I don’t like to mess with automatically generated code. And I don’t like it because PyCharm doesn’t understand it.

So, as soon as I have the new PC ready, I will try to figure out what is the best way to get the tests folder to import everything from both the current app and the dependencies, in the cleanest way possible, so PyCharm and type hinting will work without problems.

I will let you know if I figure out a pattern that makes sense to tell you about.

1 Like