You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use relative imports in generated Python packages #8738
Internally, generated Python code uses absolute imports that hard-code the assumption that users have the package installed top-level with a name like "swagger_server". For example, the following line results in the code from swagger_server import util being generated:
This prevents users from putting the code in a directory with a more semantic name of their choosing, such as "gensrc".
If the generated code instead used relative imports, e.g. from .. import util in this case, then users would be free to move the code into a directory with a name of their choosing, or even nested in a subdirectory so they could import it as from gensrc.swagger_server import util, and then all the generated code's internal imports would still work.
Swagger-codegen version
Using 2.3.1 but believe this occurs in all versions, not a regression.
Only found it mentioned in a comment on a different PR ("change to the name will not fix the problem unfortunately, we need those relative imports"): #6839 (comment)
I was running swagger-codegen-cli v2.3.1 (downloaded from maven central) on gitea's swagger template:
# 'swagger' is my own wrapper around 'swagger-codegen-cli'.
swagger \
generate -l python -i gitea/api_v1.json \
-o gitea_python_client
Two problems:
It generates a folder gitea_python_client. But the top level python module inside is named swagger_client. I'd like to be able to change that name if I could.
The generated code is broken because of its use of absolute imports. For example, in file swagger_client/models/gpg_key.py LINE 19:
This causes the Module not found runtime error.
I have commented out that line and one other line that is similar in nature (as in mirandacong/gitea_python_client@fdc4591) and it worked.
I was running swagger-codegen-cli v2.3.1 (downloaded from maven central) on gitea's swagger template:
# 'swagger' is my own wrapper around 'swagger-codegen-cli'.
swagger \
generate -l python -i gitea/api_v1.json \
-o gitea_python_client
Two problems:
It generates a folder gitea_python_client. But the top level python module inside is named swagger_client. I'd like to be able to change that name if I could.
The generated code is broken because of its use of absolute imports. For example, in file swagger_client/models/gpg_key.py LINE 19:
This causes the Module not found runtime error.
I have commented out that line and one other line that is similar in nature (as in mirandacong/gitea_python_client@fdc4591) and it worked.
Is there any solution to the first problem, specifically, is there a way now to rename swagger_client while generation or maybe after generation?
Activity
twosigmajab commentedon Sep 25, 2018
(to reiterate, passing
--model-package gensrcdoes not fix the problem as the generated code still does imports likefrom swagger_server import ...)qzmfranklin commentedon Oct 21, 2018
Same issue encountered here.
I was running swagger-codegen-cli v2.3.1 (downloaded from maven central) on gitea's swagger template:
# 'swagger' is my own wrapper around 'swagger-codegen-cli'. swagger \ generate -l python -i gitea/api_v1.json \ -o gitea_python_clientTwo problems:
It generates a folder
gitea_python_client. But the top level python module inside is namedswagger_client. I'd like to be able to change that name if I could.The generated code is broken because of its use of absolute imports. For example, in file
swagger_client/models/gpg_key.pyLINE 19:This causes the
Module not foundruntime error.I have commented out that line and one other line that is similar in nature (as in mirandacong/gitea_python_client@fdc4591) and it worked.
jupe commentedon Dec 12, 2019
I've same issue, it seems to affect only tests folder.
subtleseeker commentedon Apr 22, 2020
Is there any solution to the first problem, specifically, is there a way now to rename
swagger_clientwhile generation or maybe after generation?jake-scott commentedon Sep 30, 2024
You can change it with the -D option, eg :
-D packageName=XYZ