Ran _git rm --cached *.pyc and now No module named 'django' - python

I kept getting merge conflicts when trying to git pull from github to my production server and a lot of them had to do with the .pyc files.
So on my local machine, I ran
_git rm --cached *.pyc
git add .
git commit
git push -u origin dev
Since I had *.pyc in my .gitignore but hadn't committed properly.
Anyways, when I go to git pull on my production server, everything is pulled fine no merge conflicts.
But then I kept getting server errors on my site when trying to access the admin panel and this is the error
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
Obviously I have a virtual env working and this must be because the __pycache__ files were removed with the latest commit.
Can anyone help me? I'm not sure what to do.

I was in the wrong virtalenv...I had set up the project while I was still learning so had a few virtualenvs
I have since overhauled the old project and created a new clean VPM with only one.

Related

Why won't the runserver command in the terminal run? I am (a complete beginner) working on building a web app (learning log) with Django

So I've already created and activated a virtual environment, created a project, and an app (but the app shouldn't really matter at this point). I'm trying to view my project and ran the server command but it won't run. To clarify, I had already made more progress with the web app: created a model and migrated it so i could use it in the app, viewed the project as an admin to see the model working but when i created the second model, i had trouble migrating it. so i took a few steps back to the point where I could just view the empty project and that also didn't work so i'm confused. I could just delete and start over but I'd like to know what's wrong at this point. Below is what the terminal shows when I tried to run 'runserver' command so I could view the project on my local server.
C:\Users\acer>cd Desktop/programming/learning_log
C:\Users\acer\Desktop\programming\learning_log>ll_env\Scripts\activate
(ll_env) C:\Users\acer\Desktop\programming\learning_log>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 12, in main
raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
(ll_env) C:\Users\acer\Desktop\programming\learning_log>
Again, I had already managed to view the project through the admin site and even enter a topic (since I created a model for Topic) so obviously django is installed or at least was, and I did not delete any files or folders.

Won't import local file as module

I'm setting up a Flask application on Digitalocean and have Python 3.7 installed and the latest version of Flask. When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message:
Traceback (most recent call last):
File "application.py", line 11, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.
I've tried importing "config.py" instead of just "config" but didn't make a difference. I also tried specifying exactly what it should import (instead of using '*') but that didn't make a difference either.
Your thoughts on why it can't find config?
What seems to have been the solution to my problem above was to run the Python shell and add the path to the directory in which config.py is located (even though it's in the same folder as application.py...) by using the following command:
sys.path.append("/path/to/config/")

Pushed Django project to GitHub with Pycharm - now not working

Sorry for the generic question; I'm not sure how to best classify the problem.
I had a Django project that I was working on locally, then I pushed up to GitHub to collaborate with someone (I'm completely new to GitHub). I did this using pycharm and the project was copied into a subdirectory of PycharmProjects. It seemed to work fine. I didn't work on it for a week or so, during which time I upgrated to Catalina.
I've come back to work on the project and it's completely broken! I can't get the development server up and running from either the PycharmProjects directory or my original working directory on the desktop. I suspect in the PycharmProjects folder it's a path or dependency problem, but I didn't change anything in my original folder.
I can activate the virtual environment, then I get these errors:
(ll_env) Davids-MacBook-Pro-2:POM_Journal_Club davidhallsworth$ python manage.py runserver
File "manage.py", line 16
) from exc
^
SyntaxError: invalid syntax
(ll_env) Davids-MacBook-Pro-2:POM_Journal_Club davidhallsworth$ python3 manage.py runserver
Traceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 16, in main
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
The repository is available at https://github.com/dchallsworth/POM_Journal_Club
Any help would be massively appreciated!! It's my first project and has taken a lot of time to learn.

ImportError: No module named django, sys.path fine

I'm trying to configure Django with
python manage.py shell
And I get the error:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/usr/lib64/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib64/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/usr/lib64/python2.7/site-packages/django/apps/config.py", line 123, in create
import_module(entry)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named django
I've seen a lot of questions about this error, and most of them seem to be centered on the fact that django's path is not included in python's sys.path. Mine is, however. When I run
sudo pip install django
I get
Requirement already satisfied: django in /usr/lib64/python2.7/site-packages
And the output for my sys.path is
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
So the path is absolutely there.
What are my options here? This is a remote interpreter using SSH from Pycharm, and it's seriously delaying my work.
If which python, when run by the normal user, returns ~/bin/python, then this is a different Python from the one run by the superuser, which, according to what you say, is /bin/python. sudo pip install django runs as the superuser and installs Django in the system's Python environment. When, afterwards, you execute python manage.py shell, it runs another Python, and that other Python's environment apparently doesn't have Django installed.
If you run pip install django without the sudo it might work, but since you have superuser permission on that machine it would be better to get rid of the Python installation that you have in the normal user's home directory, and make sure your system has only one Python installation until you really know what you are doing. Otherwise you will be confused whether you use virtualenv or not.
Edit: I disagree with many comments that tell you to use virtualenv. You are confused enough without it. Let's simplify the problem first. Remove all virtualenvs you have created (you do this by deleting the directories) and forget everything about virtualenv. Logout and login again (this will ensure the virtualenvs are deactivated). Don't modify your manage.py, it's fine as it was originally. Install Django system-wide with sudo pip install django, then try to run your Django project with python manage.py shell. Only after you get it working and you start having a grip on the system go on to start playing with virtualenv. Virtualenv is great, but everything in its time. (virtualenv demystified is an introduction to virtualenv written by me.)

Gunicorn+Django+Heroku. Python Path Issue

I'm running a Django app that I used to deploy to Heroku with Gunicorn without a hitch. By that I mean that I could run
foreman start
on my localhost and be able to understand whether the application would work in the production environment. I recently made the decision to move all of our project's applications into a subfolder named 'applications'. This meant editing my DJANGO_SETTINGS_MODULE environment variable among other files. After doing this, I experimented with foreman and gunicorn until I got the local server up and running. At this point I deployed to production. Needless to say, there was an error:
Running `gunicorn --workers=4 applications.rocketlistings.wsgi -b 0.0.0.0: -k gevent` attached to terminal... up, run.8052
Traceback (most recent call last):
File "/app/.heroku/python/bin/gunicorn", line 9, in <module>
load_entry_point('gunicorn==0.17.2', 'console_scripts', 'gunicorn')()
File "/app/.heroku/python/lib/python2.7/site-packages/distribute-0.6.34-py2.7.egg/pkg_resources.py", line 343, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/app/.heroku/python/lib/python2.7/site-packages/distribute-0.6.34-py2.7.egg/pkg_resources.py", line 2307, in load_entry_point
return ep.load()
File "/app/.heroku/python/lib/python2.7/site-packages/distribute-0.6.34-py2.7.egg/pkg_resources.py", line 2013, in load
entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named app.wsgiapp
I'm not sure what I'm doing wrong, and it's very difficult to debug the situation given the obscure remote setup heroku runs. Other threads discussing this error suggest that this is a path problem. This sounds very plausible, but I can't think of what could be wrong with the path, given that I have an init.py file in my applications folder, and everything works locally.
Here is my production path/pythonpath anyway:
Running `echo $PATH` attached to terminal... up, run.2583
/app/.heroku/python/bin:/usr/local/bin:/usr/bin:/bin
Running `echo $PYTHONPATH` attached to terminal... up, run.8026
/app/
Thanks in advance!
Sometimes, this occurs when Gunicorn finds another module called 'app' in the PYTHONPATH. Setting PYTHONPATH to a fake value resolves the issue:
$ heroku config:set PYTHONPATH=fakepath
https://github.com/heroku/heroku-buildpack-python/wiki/Troubleshooting#no-module-named-appwsgiapp

Categories