diff --git a/pydantic/typing.py b/pydantic/typing.py
--- a/pydantic/typing.py
+++ b/pydantic/typing.py
@@ -289,10 +289,16 @@ def resolve_annotations(raw_annotations: Dict[str, Type[Any]], module_name: Opti
 
     Resolve string or ForwardRef annotations into type objects if possible.
     """
+    base_globals: Optional[Dict[str, Any]] = None
     if module_name:
-        base_globals: Optional[Dict[str, Any]] = sys.modules[module_name].__dict__
-    else:
-        base_globals = None
+        try:
+            module = sys.modules[module_name]
+        except KeyError:
+            # happens occasionally, see https://github.com/samuelcolvin/pydantic/issues/2363
+            pass
+        else:
+            base_globals = module.__dict__
+
     annotations = {}
     for name, value in raw_annotations.items():
         if isinstance(value, str):

diff --git a/tests/test_edge_cases.py b/tests/test_edge_cases.py
--- a/tests/test_edge_cases.py
+++ b/tests/test_edge_cases.py
@@ -1,3 +1,4 @@
+import importlib.util
 import sys
 from collections.abc import Hashable
 from decimal import Decimal
@@ -1773,3 +1774,22 @@ def get_double_a(self) -> float:
     assert model.a == 10.2
     assert model.b == 10
     return model.get_double_a() == 20.2
+
+
+def test_resolve_annotations_module_missing(tmp_path):
+    # see https://github.com/samuelcolvin/pydantic/issues/2363
+    file_path = tmp_path / 'module_to_load.py'
+    # language=Python
+    file_path.write_text(
+        """
+from pydantic import BaseModel
+class User(BaseModel):
+    id: int
+    name = 'Jane Doe'
+"""
+    )
+
+    spec = importlib.util.spec_from_file_location('my_test_module', file_path)
+    module = importlib.util.module_from_spec(spec)
+    spec.loader.exec_module(module)
+    assert module.User(id=12).dict() == {'id': 12, 'name': 'Jane Doe'}

Issue with BaseModel if used with importlib 
### Checks

* [x] I added a descriptive title to this issue
* [x] I have searched (google, github) for similar issues and couldn't find anything
* [x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this is a bug

<!-- Sorry to sound so draconian, but every second saved replying to issues is time spend improving pydantic :-) -->

# Bug

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.7.3
            pydantic compiled: True
                 install path: /opt/conda/lib/python3.8/site-packages/pydantic
               python version: 3.8.2 (default, Mar 25 2020, 17:03:02)  [GCC 7.3.0]
                     platform: Linux-4.15.0-130-generic-x86_64-with-glibc2.10
     optional deps. installed: ['typing-extensions']
```

If I'm trying to load a module using `importlib` where a BaseModel is used then execution is crashed with the following traceback:
```
Traceback (most recent call last):
  File "loader.py", line 13, in <module>
    m = load_module("module_to_load.py")
  File "loader.py", line 7, in load_module
    spec.loader.exec_module(module)  # type: ignore[union-attr]
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "module_to_load.py", line 4, in <module>
    class User(BaseModel):
  File "pydantic/main.py", line 274, in pydantic.main.ModelMetaclass.__new__
  File "pydantic/typing.py", line 293, in pydantic.typing.resolve_annotations
    def __copy__(self):
```

How to repro:
1) create `loader.py` file with the following content
```python
import importlib.util

def load_module(fpath):
    spec = importlib.util.spec_from_file_location("mymodule", fpath)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)  # type: ignore[union-attr]
    return module

if __name__ == "__main__":    
    m = load_module("module_to_load.py")
    print(m)
    m.foo()
```

2) create `module_to_load.py` : 
```python
from pydantic import BaseModel

class User(BaseModel):
    id: int
    name = 'Jane Doe'

def foo():
    print(User(id=12, name="test"))
```

3) Run `python loader.py`


As use-case may seem strange, I'm a bit hestiating between a bug and wrong usage, but in the latter case, error message is unclear at all. Important part is that I have to import the module in this way and not in any other more standard python way in which pydantic indeed work without any issue.
Thanks 


EDIT:

Indeed, if we add `sys.modules["mymodule"] = module` to `load_module` as suggested [here](https://docs.python.org/3/library/importlib.html?highlight=exec_module#importing-a-source-file-directly), there is no issue, anymore.
I have to figure out what would be the impact of doing that for my goal.


tests/test_edge_cases.py::test_resolve_annotations_module_missing
tests/test_edge_cases.py::test_str_bytes tests/test_edge_cases.py::test_str_bytes_none tests/test_edge_cases.py::test_union_int_str tests/test_edge_cases.py::test_union_priority tests/test_edge_cases.py::test_typed_list tests/test_edge_cases.py::test_typed_set tests/test_edge_cases.py::test_dict_dict tests/test_edge_cases.py::test_none_list tests/test_edge_cases.py::test_typed_dict[value0-result0] tests/test_edge_cases.py::test_typed_dict[value1-result1] tests/test_edge_cases.py::test_typed_dict[value2-result2] tests/test_edge_cases.py::test_typed_dict_error[1-errors0] tests/test_edge_cases.py::test_typed_dict_error[value1-errors1] tests/test_edge_cases.py::test_typed_dict_error[value2-errors2] tests/test_edge_cases.py::test_dict_key_error tests/test_edge_cases.py::test_tuple tests/test_edge_cases.py::test_tuple_more tests/test_edge_cases.py::test_tuple_length_error tests/test_edge_cases.py::test_tuple_invalid tests/test_edge_cases.py::test_tuple_value_error tests/test_edge_cases.py::test_recursive_list tests/test_edge_cases.py::test_recursive_list_error tests/test_edge_cases.py::test_list_unions tests/test_edge_cases.py::test_recursive_lists tests/test_edge_cases.py::test_str_enum tests/test_edge_cases.py::test_any_dict tests/test_edge_cases.py::test_success_values_include tests/test_edge_cases.py::test_include_exclude_unset tests/test_edge_cases.py::test_include_exclude_defaults tests/test_edge_cases.py::test_skip_defaults_deprecated tests/test_edge_cases.py::test_advanced_exclude tests/test_edge_cases.py::test_advanced_exclude_by_alias tests/test_edge_cases.py::test_advanced_value_include tests/test_edge_cases.py::test_advanced_value_exclude_include tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude0-expected0] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude1-expected1] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude2-expected2] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude3-expected3] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude4-expected4] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude5-expected5] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude6-expected6] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude7-expected7] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude8-expected8] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude9-expected9] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude10-expected10] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude11-expected11] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude12-expected12] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude13-expected13] tests/test_edge_cases.py::test_advanced_exclude_nested_lists[exclude14-expected14] tests/test_edge_cases.py::test_advanced_include_nested_lists[include0-expected0] tests/test_edge_cases.py::test_advanced_include_nested_lists[include1-expected1] tests/test_edge_cases.py::test_advanced_include_nested_lists[include2-expected2] tests/test_edge_cases.py::test_advanced_include_nested_lists[include3-expected3] tests/test_edge_cases.py::test_advanced_include_nested_lists[include4-expected4] tests/test_edge_cases.py::test_advanced_include_nested_lists[include5-expected5] tests/test_edge_cases.py::test_advanced_include_nested_lists[include6-expected6] tests/test_edge_cases.py::test_advanced_include_nested_lists[include7-expected7] tests/test_edge_cases.py::test_advanced_include_nested_lists[include8-expected8] tests/test_edge_cases.py::test_advanced_include_nested_lists[include9-expected9] tests/test_edge_cases.py::test_advanced_include_nested_lists[include10-expected10] tests/test_edge_cases.py::test_advanced_include_nested_lists[include11-expected11] tests/test_edge_cases.py::test_advanced_include_nested_lists[include12-expected12] tests/test_edge_cases.py::test_advanced_include_nested_lists[include13-expected13] tests/test_edge_cases.py::test_advanced_include_nested_lists[include14-expected14] tests/test_edge_cases.py::test_field_set_ignore_extra tests/test_edge_cases.py::test_field_set_allow_extra tests/test_edge_cases.py::test_field_set_field_name tests/test_edge_cases.py::test_values_order tests/test_edge_cases.py::test_inheritance tests/test_edge_cases.py::test_invalid_type tests/test_edge_cases.py::test_valid_string_types[a tests/test_edge_cases.py::test_valid_string_types[some tests/test_edge_cases.py::test_valid_string_types[value2-foobar] tests/test_edge_cases.py::test_valid_string_types[123-123] tests/test_edge_cases.py::test_valid_string_types[123.45-123.45] tests/test_edge_cases.py::test_valid_string_types[value5-12.45] tests/test_edge_cases.py::test_valid_string_types[True-True] tests/test_edge_cases.py::test_valid_string_types[False-False] tests/test_edge_cases.py::test_valid_string_types[a10-a10] tests/test_edge_cases.py::test_valid_string_types[whatever-whatever] tests/test_edge_cases.py::test_invalid_string_types[value0-errors0] tests/test_edge_cases.py::test_invalid_string_types[value1-errors1] tests/test_edge_cases.py::test_inheritance_config tests/test_edge_cases.py::test_partial_inheritance_config tests/test_edge_cases.py::test_annotation_inheritance tests/test_edge_cases.py::test_string_none tests/test_edge_cases.py::test_return_errors_ok tests/test_edge_cases.py::test_return_errors_error tests/test_edge_cases.py::test_optional_required tests/test_edge_cases.py::test_invalid_validator tests/test_edge_cases.py::test_unable_to_infer tests/test_edge_cases.py::test_multiple_errors tests/test_edge_cases.py::test_validate_all tests/test_edge_cases.py::test_force_extra tests/test_edge_cases.py::test_illegal_extra_value tests/test_edge_cases.py::test_multiple_inheritance_config tests/test_edge_cases.py::test_submodel_different_type tests/test_edge_cases.py::test_self tests/test_edge_cases.py::test_self_recursive[BaseModel] tests/test_edge_cases.py::test_self_recursive[BaseSettings] tests/test_edge_cases.py::test_nested_init[BaseModel] tests/test_edge_cases.py::test_nested_init[BaseSettings] tests/test_edge_cases.py::test_values_attr_deprecation tests/test_edge_cases.py::test_init_inspection tests/test_edge_cases.py::test_type_on_annotation tests/test_edge_cases.py::test_assign_type tests/test_edge_cases.py::test_optional_subfields tests/test_edge_cases.py::test_not_optional_subfields tests/test_edge_cases.py::test_scheme_deprecated tests/test_edge_cases.py::test_fields_deprecated tests/test_edge_cases.py::test_optional_field_constraints tests/test_edge_cases.py::test_field_str_shape tests/test_edge_cases.py::test_field_type_display[int-int] tests/test_edge_cases.py::test_field_type_display[type_1-Optional[int]] tests/test_edge_cases.py::test_field_type_display[type_2-Union[NoneType, tests/test_edge_cases.py::test_field_type_display[type_3-Union[int, tests/test_edge_cases.py::test_field_type_display[type_4-List[int]] tests/test_edge_cases.py::test_field_type_display[type_5-Tuple[int, tests/test_edge_cases.py::test_field_type_display[type_6-Union[List[int], tests/test_edge_cases.py::test_field_type_display[type_7-List[Tuple[int, tests/test_edge_cases.py::test_field_type_display[type_8-Mapping[int, tests/test_edge_cases.py::test_field_type_display[type_9-FrozenSet[int]] tests/test_edge_cases.py::test_field_type_display[type_10-Tuple[int, tests/test_edge_cases.py::test_field_type_display[type_11-Optional[List[int]]] tests/test_edge_cases.py::test_field_type_display[dict-dict] tests/test_edge_cases.py::test_field_type_display[type_13-DisplayGen[bool, tests/test_edge_cases.py::test_any_none tests/test_edge_cases.py::test_type_var_any tests/test_edge_cases.py::test_type_var_constraint tests/test_edge_cases.py::test_type_var_bound tests/test_edge_cases.py::test_dict_bare tests/test_edge_cases.py::test_list_bare tests/test_edge_cases.py::test_dict_any tests/test_edge_cases.py::test_modify_fields tests/test_edge_cases.py::test_exclude_none tests/test_edge_cases.py::test_exclude_none_recursive tests/test_edge_cases.py::test_exclude_none_with_extra tests/test_edge_cases.py::test_str_method_inheritance tests/test_edge_cases.py::test_repr_method_inheritance tests/test_edge_cases.py::test_optional_validator tests/test_edge_cases.py::test_required_optional tests/test_edge_cases.py::test_required_any tests/test_edge_cases.py::test_custom_generic_validators tests/test_edge_cases.py::test_custom_generic_arbitrary_allowed tests/test_edge_cases.py::test_custom_generic_disallowed tests/test_edge_cases.py::test_hashable_required tests/test_edge_cases.py::test_hashable_optional[1] tests/test_edge_cases.py::test_hashable_optional[None] tests/test_edge_cases.py::test_default_factory_side_effect tests/test_edge_cases.py::test_default_factory_validator_child tests/test_edge_cases.py::test_cython_function_untouched