Context Problem in Blender 2.80 python module I run Blender 2.80 as python module (for running on a server with Django), so there is no gui. 
Apparently passing the correct context doesn't seem to work for me in this case.
I have tried many suggested implementations to override context (a lot were for Blender 2.7), but i still get context errors for most operators
For example the line: bpy.ops.object.mode_set(mode='EDIT') 
with or without     ...(override, ...

produces the error


  bpy.ops.object.mode_set.poll() failed, context is incorrect


and I cannot search through the contexts until for example the correct area is found:

for area in bpy.context.window.screen.areas:
    if area.type == 'VIEW_3D':
        v3d = area.spaces[0]
        rv3d = v3d.region_3d
        for region in area.regions:
            if region.type == 'WINDOW':
                break            

override = {
    'scene'            : bpy.context.scene,
    'region'           : region,
    'area'             : area,
    'space'            : v3d
}


as this will produce the error:  


  'NoneType' object has no attribute 'screen'


When running the following Code:

print(bpy.context.window)
print(bpy.context.mode)
print(bpy.context.area)
print(bpy.context.region)
print(bpy.context.scene)


the output I get is:

None 
OBJECT 
None 
None 
&lt;bpy_struct, Scene("Scene")&gt;


So maybe there is no fully set context to begin with? Could it be an option to make a new context?

The problem occurs even when i have a .py (with only some other Django methods) where i use import bpy at the start of the file and do bpy.ops.object.mode_set(mode='EDIT') in an if statement that is activated by django at the program startup.

However this code example works normally and exports a .stl file with a rounded cube:

import bpy
bpy.ops.object.select_all(action='SELECT')
bpy.data.objects.remove(bpy.data.objects["Cube"], do_unlink=True)
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.mesh.primitive_cube_add()
myObj = bpy.context.object
myObj.select_set(True)
bpy.context.view_layer.objects.active = myObj
myObj.location = ( 0, 1, 1)
myObj.scale = ( 1/2, 1/2, 1/2)
mySubsurfMod = myObj.modifiers.new(myObj.name, 'SUBSURF')
mySubsurfMod.levels = 4
bpy.ops.export_mesh.stl(filepath="myExportSTL.stl")


Does someone know how to make override context work in blender 2.80?
Or maybe at least a good resource for deep level commands to avoid operators? Or is there maybe even a way to see the deep level commands which are accessed by the operators (similar to the info screen) in blender?

Thanks in advance!!!
Some things to check and look out for.


Expect context.window, context.screen etc to be None in headless
mode. 
If an operator works in the python console in blender then chances
are it needs no special window, screen or region for context.  AFAIK
the edit mode toggle operator only needs an editable object active.
Check the bpy module works in your python shell 

Recommend using myObj = context.object A new cube may not always have
the name "Cube" eg Cube in another language
The space member of context is context.space_data or from the area as area.spaces.active.


And the answer was, as supplied by the OP


  ...It seems, that the precise location where i do the import bpy is
  very important. My error was that I put import bpy at the top of my
  .py (prior to some Django getUserInput function). Therefore i strongly
  recommend to others working in Django or other python environments to
  always use import bpy DIRECTLY before your blender scripting. I have
  now also tried to run a more advanced script and it works!