Skip to content

Error connect to database when sys.argv is not present (using python c api) #736

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

Closed
yhojann-cl opened this issue Oct 27, 2018 · 0 comments · Fixed by #739
Closed

Error connect to database when sys.argv is not present (using python c api) #736

yhojann-cl opened this issue Oct 27, 2018 · 0 comments · Fixed by #739

Comments

@yhojann-cl
Copy link

yhojann-cl commented Oct 27, 2018

For example in c++ project using python3 c api, does not exist the sys.argv object when call pymysql.connect(). From C api call as module, is not called from cli/interpreter.

For /usr/local/lib/python3.6/site-packages/pymysql/connections.py in line 321:

if program_name:
    self._connect_attrs["program_name"] = program_name
elif sys.argv:
    self._connect_attrs["program_name"] = sys.argv[0]

Error message:

module 'sys' has no attribute 'argv'.
File "/....py", line 37, in mysqlConnect
File "/usr/local/lib/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
  return Connection(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 321, in __init__
  elif sys.argv:

The solution for me: Simulate the argv:

import pymysql

# For pymysql bug
import sys
import os
sys.argv=[os.path.realpath(__file__)]
...

You need replace:

elif sys.argv:

By:

elif callable(getattr(sys, 'argv')) and sys.argv:
    ...
else:
    ... Canot get the application name exception or set a application name by default.

Realy need the python script path for a mysql connection?

methane added a commit to methane/PyMySQL that referenced this issue Oct 27, 2018
methane added a commit to methane/PyMySQL that referenced this issue Oct 27, 2018
methane added a commit that referenced this issue Oct 29, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant