Forums

can not run python manage.py migrate

I am trying to make migration to mysql database nut the bash returned l=this script

 File "/home/jambari/my-first-blog/myvenv/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/jambari/my-first-blog/myvenv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/jambari/my-first-blog/myvenv/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/home/jambari/my-first-blog/myvenv/lib/python3.4/site-packages/django/db/backends/base/base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/jambari/my-first-blog/myvenv/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 264, in get_new_connection
    conn = Database.connect(**conn_params)
  File "/home/jambari/my-first-blog/myvenv/lib/python3.4/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/home/jambari/my-first-blog/myvenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

what should I do ?

[edited by admin: formatting]

what are the database settings that you are using to connect?

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'blog', 'USER': 'jambari', 'PASSWORD': '**', 'HOST': 'localhost', 'PORT': '', } }

My pythonanywhere mysql username = jambari

If you are trying to connect to the pythonanywhere mysql database, you would neet to set the host to the hostname in the databases tab on pythonanywhere.

Hello, I'm getting the same error only when I try to run "python manage.py migrate". However, when a hit the green button to reload my site, it runs perfectly, and I login into the admin area, I'm able to manage my data, adding, editing and deleting with the tools provided by the admin app. Migrate also runs on my local machine. Can you tell me what settings could interfere with migrations? The workflow I'm establishing, is: Local machine: develop and git push to remote (bitbucket) Pythonanywhere: git pull and run it...

When in the develop stage (local machine) I do any changes or updates to my data models.py, it is necessary to migrate in both servers, right? As we have two diferente databases running, right? Any other sugestions?

I've installed a virtualenv, named ENV, with Python 3.4 and Django 1.9.8, because that's what are running on my machine, and that are the versions recomended in a offical tutorial I'm been folowing.

Apreciate any help. thanks.

You're right, you do need to run the migration on both your local machine and on PythonAnywhere. It's certainly strange if you're getting an error that mentions mysqld.sock from the migration if the admin works. Can I take a look at your code? We can see it from our side, but we always ask for permission first.

Yes you can, please do! I also have another question about virtualenv. Is the place (path) where we put/create the virtual env important? I mean, can I create the virtual outside my project, or can I use the same virtualenv with multiple projects? E.g. mySite, yourSite and hisSite, can run separately, using the same ENV?

Other point: wsgi.py references settings.py (or local_settings.py in my case), and settings.py is the only place where is a reference to database, right? Thanks.

OK, I can see the problem with the website. You have two settings files, one with the correct database settings (local_settings.py), and one with localhost as the database. In your WSGI file you're specifying that settings should come from local_settings.py, which is why the website works. But when you run the migration, it will default to picking up the one called settings.py, which is why it doesn't work. The WSGI file only applies when you're accessing the website, not in bash consoles.

If you run

export DJANGO_SETTINGS_MODULE=contactos.local_settings

...before running the migration, it should work.

Regarding your virtualenv questions -- you can put it anywhere you like, and you can use the same one with multiple virtualenvs, yes.

Hi, Your answers are awesome! Thanks for the free lesson, I think I've learned a lot today!

I will correct the thing, so I can go forward in the programming stuff...

Definitely settings.py is to be in the .gitignore file... ;-) and now, I know (and understood) why!!!

Thanks again.

Hi, I am new to 'pythonanywhere' and I am just deploying my sample work. I pulled my repository from git and now trying to run 'Migration' on Bash Console. But I am getting this "Import Error: Cannot import name 'path'". Could anyone please help me out with this?

Hi there, do any of the tips here help? https://help.pythonanywhere.com/pages/DebuggingImportError

Hi guys, how are u doing?

I have these lines at the end of my settings.py:

try:
    from local_settings import * 
except ImportError:
    print('local_settings.py not found')

but when i run any manage.py command in the console I get the ImportError 'local_settings.py not found' although i created the local_settings.py in the same folder with settings.py... am i missing something?

P.S. I am using the same method in our servers in my day job and i never faced such issue.

Interesting. Does changing it to

try:
    from .local_settings import * 
except ImportError:
    print('local_settings.py not found')

...help? (Note the "." in front of the local_settings.)