-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use relative imports in generated Python packages #8738
Comments
(to reiterate, passing |
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_client Two problems:
from swagger_client.models.gpg_key import GPGKey # noqa: F401,E501 This causes the |
I've same issue, it seems to affect only tests folder. |
Is there any solution to the first problem, specifically, is there a way now to rename |
You can change it with the -D option, eg : |
Description
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:https://github.com/swagger-api/swagger-codegen/blob/62b93fc/modules/swagger-codegen/src/main/resources/flaskConnexion/controller.mustache#L6
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 asfrom 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.
Steps to reproduce
Related issues/PRs
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)
Suggest a fix/enhancement
Use relative imports.
The text was updated successfully, but these errors were encountered: