templates cannot be found when virtual environment is activated - python

I am facing a rather peculiar problem. I have built a Django application and only while deploying it, I created a virtual environment(I didn't use virtual environment while building and testing it locally). Now when virtual environment is activated and also when deactivated, the HTML templates cannot be found. Below is the screenshot.
Seeing this statement,
Python Executable: /home/shadowsaint/Programs/Django/zoomtail/venv/bin/python
I went back to terminal and typed which python command. For that it shows
/home/shadowsaint/Programs/Django/zoomtail/venv/bin/python
Now when I deactivate virtual environment also the which python command gives the same thing.
But when I delete the venv file, and run which python it gives,
/usr/bin/python
And now the application runs fine, i.e., the HTML templates are being found.
I have three questions here.
When I activate venv, the python used from venv/bin is fine. But even when I deactivate virtual environment why the same python is being used?
What does python package(both the versions are same) have to do with templates being found?
Is the underlying principle here the same as that in here too - No module named PIL in heroku though it is installed

You have a misconfiguration at your Heroku environment, first check if your requirements was installed, use shell (python manage.py shell) and try import some of them to see if they throw errors... if passes ok then try to access any url from your project... if template breaks maybe the problem is in your Template_DIR at your settings
Obs.: check if your django commands like shell, migrate... works when running heroku run bash with your venv activated
Try follow some of this steps:
https://devcenter.heroku.com/articles/getting-started-with-python#introduction
http://docs.python-guide.org/en/latest/dev/virtualenvs/

Verify in project settings.py that INSTALLED_APPS list actually contains your app name.
INSTALLED_APPS = [
'your app',
'django.contrib...'
.....

Related

Deploying flask including created venv by manipulating pyvenv.cfg

I'm new to python and venv and I think I'm misunderstanding the design of venv here.
I've created a flask project with python 3.8 locally(win 10) and planned to deploy to a server(windows server 2016) that DOES NOT have python 3.8 installed.
I've created virtual environment locally with below cmd:
python -m venv env
After checking the pyvenv.cfg file, the home key shows:
home = local\absolute\path\to\python\used\to\create\venv
As expected, I copied the whole project folder to the server and activate the venv returns me with
no python at local\absolute\path\to\python\used\to\create\venv
So I changed the pyvenv.cfg home key to the python interpreter in the deploy venv
home = deploy\absolute\path\to\Scripts\dir\in\venv
Yet if I run activate.bat and start my flask project, the process just hanged there and flask isn't responding.
Besides, If I simply run activate.bat and type in python, it just hangs there and not even the python version is showing up. My problems are:
Is manually changing the home key in pyvenv.cfg to the interpreter included in the venv a good practice to get the project running when target python version is not installed?
If yes, what are the possible reasons causing my process to halt? I assume that everything needed to run python are included in the venv.
If not, why doing so causes the process to halt?
Or should I get python 3.8 installed instead? Since i found some saying the only way to get it running is through pyinstaller if python is not installed, but wrapping whole flask app to an exe doesn't seem practical
Any help is appreciated, thanks in advance!

no python at "C:\Users\AccountName\AppData\Local\Programs\Python\Python38-32\python.exe" error in VsCode

I was running the Django project without any problems. Until I reinstalled Windows and then reinstalled vscode! Now that I am running the Django project, vscode shows the following error:
Error: no python at C:\Users\AccountName\AppData\Local\Programs\Python\Python38-32\python.exe
This might be occurs if the python directory still in the environment variables path list.
First remove python entry from the environment variables path if exists.
Also there will be a chance for virtual environments in your project. If the virtual environment is setup in the uninstalled python version then you will get the same error message.
So, remove the virtual environments which is created under the uninstalled python. It can be done by deleting the virtual environment folder from your system.
Edit pyvenv.cfg
home = C:\Users\UserName\AppData\Local\Programs\Python\Python38-32
include-system-site-packages = false
version = 3.8.3
When I edited this file after checking the file path on my computer, it worked for me.
Based on the information you provided, it is recommended that you check the Python environment variables.
Since the Windows system is reinstalled, the environment variables are restored to the default settings. Therefore, please add Python environment variables:
Or you can reinstall Python and check the "Add Python 3.8 to PATH" option, which will automatically add Python environment variables.
You could also refer to : Python environment variables.
It's very simple
Delete your dbsqite3 file from your project folder
Open your project in CMD
install virtual environment pip install virtaualenv
virtualenv .
\scripts\activate
python manage.py runserver
and play with your code
you shared.
This works for Pycharm, It did to me
From your projects folder, delete the 3 folders i.e. .ide,venv, and the third folder i think.. do not touch the projects or scripts you created. After wards, go to Pycharm and configure python interpreter again. This will now enable you to run yo scripts now with no prob
make sure to install virtualenv befor install python
check wheather django is installed (if your are using django framework).
pip install django

How to debug Django while using pipenv and VS Code?

I'm been using VS code and venv for my Django projects and have been able to debug using the tutorial provided by Microsoft here.
https://code.visualstudio.com/docs/python/tutorial-django
However, I have now switched to pipenv instead which I activate using the commmand pipenv shell.
When I now run using the Django debug configuration I get an error.
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?
I can't find any resources to guide me on this.
Do I need to change the launch.json file to make it work with pipenv?
you just need to choose an interpreter
in my case, all pipenv interpreters are located in the ~/.virtualenvs folder
here is the screenshot

How to change incorrect Django Path (django.__path__)?

I have pip installed django into my project and now it seems that my Django Project is confused about what Django installation should it be running.
When I run
python manage.py shell
and
import django
django.__path__
Shell returns incorrect path:
['D:\\project_2\\venv\\lib\\site-packages\\django']
Since I am working on project_1, I expect the following path:
['D:\\project_1\\venv\\lib\\site-packages\\django']
Is there a quick fix?
Are you sure you are using the right virtual environment ?
If using conda a better practice is to do environment by project,
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html,
Because the Django path refers to the location where your environment is setup

Flask runs out of virtual environment

I was working in virtual environment but I removed the virtual environment folder in purpose. But I still can run flask restful application as below, while new empty virtual environment(there are no libraries in site packages but even application works like old virtual environment) is active.
flask run
I thought it should have been failed and complaining like:
no module named flask
, because I removed the old proper virtual environment and installed new one then activated it.
And here is the point, when I try the run the main script as below
python3 app.py
It complains as expected:
no module named flask
It seems that when I run with flask run it is not using virtual environment even virtual environment is active. Because of this reason I can't test my application with other python versions and can't test if requirements are installed and run in new other virtual environments. I couldn't understand this behavior of the flask for any help I appreciate.
Thanks in advance.
You may have Flask globally installed.
pip list | grep flask
You can remove that:
pip uninstall flask
or
Create virtualenv with --no-site-packages option:
virtualenv --no-site-packages venv
source venv/bin/activate
Well I figured out what is going on there.
Flask creates a directory named local in home and moved system's interpreter and dependencies there included by flask application that I wrote.
Then it deploys the application from there when run with flask run. So that means my flask app was not running with virtual environment as we guess.
So in that case I recreated my virtual environment and after installing dependencies I run with python3 app.py. Now it seems that it uses the virtual environment dependencies and interpreter.

Categories