python virtualenv module import error - python

I am using virtualenv and have activated it and tested it like this:
source .virtualenvs/myapp/bin/activate
pip freeze
(myapp) me: redis==2.05
Then checking redis can import OK:
(myapp) me: python
>>>import redis
This works OK.
However on running the following
(myapp) me: sudo ./manage.py database create
I get the following error:
Traceback (most recent call last):
File "./manage.py", line 4, in <module>
from myapp import manager
File "/home/me/myapp/__init__.py", line 1, in <module>
import redis
ImportError: No module named redis
Redis is clearly installed for the virtualenv, any thoughts on what could be going wrong?

I suspect that sudo is the reason. It does not properly preserve the virtualenv.
Just try to create a shell script, which first sources the environment and than executes python. Than run this shell script with the sudo command:
#!/bin/sh
# ensure that working directory is right
source .virtualenvs/myapp/bin/activate
./manage.py database create
and run it:
$ chmod ugo+x my-startup-shell.sh
$ sudo my-startup-shell.sh
If that works, do some experiments with sudo like possibly passing the -E option to inherit the environment etc.

Related

How can I fix a problem with runing conda django virtual enviroment?

Can anyone help me with following problem? I encountered that during one of the courses I am taking.
I use ios Catalina 10.15 therefor I use zhs as bash is no longer supported by mac
I am trying to run virtual server so I can see my webpage
I managed to run the virtual server once only (python manage.py runserver) and after the restart I have problem with it eversince.
> first_proj 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 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?
when I check for django (django-admin --version), turns out it is installed, I am getting this ouput:
2.2.5 (version of Django installed).
my django directory is: /Users/myname/opt/anaconda3/envs/projectname/lib/python3.8/site-packages/django
maybe it is something about the directory django is installed (?)
thanks for any help.
First of all I recommend you to install Anaconda in /opt/anaconda3, not in /somepath/opt/anaconda3. And when you install it, accept the auto-init.
Second, Zsh and Anaconda must be configured separately so they can work together. Add Anaconda to your paths (/etc/paths). You can check to see if everything is going well by applying any conda command:
$ conda info -e
Third, remember to activate your environment before applying runserver.
$ source activate <conda-env-name>
Using this command, you can see all the libraries installed in your environment: (and check if django is there)
(<conda-env-name>) username#mac: ~ $ conda list -e
And then make runserver:
(<conda-env-name>) username#mac: ~ $ python3 manage.py runserver

How to run GitHub Code quickly when your environment missing modules?

I would like to run the following project:
https://github.com/pyl0ne/flaskSaaS
At the 2nd step of executing causes the error
python manage.py initdb (if I run it like this it has no problem, but he have run it with python3 with no problem as well: https://youtu.be/NzmoPqte4V4?t=1623 )
I Do want to run it with python 3 so instead I use:
python3 manage.py initdb
To run it in python3 I have corrected in the code manage file:
ORIGINAL (tried with this one before): from flask.ext.script import Manager, prompt_bool, Shell, Server
Mine: from flask_script import Manager, prompt_bool, Shell, Server
based on: importerror: no module named flask.ext.script
I have tried to run it in PyCharm directly and checked the DE's interpreter all set to python 3.7 importerror: no module named flask.ext.script (It gives errors similar to the terminal)
Final Error:
Beli:flaskSaaS-master peterSimon$ python3 manage.py runserver
Traceback (most recent call last):
File "manage.py", line 1, in <module>
from flask_script import Manager, prompt_bool, Shell, Server #ORIGINAL: from flask.ext.script import Manager, prompt_bool, Shell, Server
ModuleNotFoundError: No module named 'flask_script'
You can get rid of confusion by using a virtual environment. So you only have one Python interpreter (version 3.x) and one pip version for this Python version:
git clone git#github.com:pyl0ne/flaskSaaS.git
cd flaskSaaS/
python3 -m venv venv
source venv/bin/activate
make install
python manage.py initdb
should work.
Check if make install actually installed flask_script.
If not, do pip install Flask-Script
and for python3
pip3 install Flask-Script

Remove sudo to run python script

Every time that I need to run my python program with:
python my_program.py
I get some error saying that some import was not found.
Some error like this:
Traceback (most recent call last):
File "graphic.py", line 1, in <module>
import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib'
Than I run:
sudo python my_program.py
And every thing works just fine. How do I remove the sudo command to run my python codes?
ImportError: No module named 'matplotlib' happens when your Python doesn't find the module. sudo changes the enviornment variables; That's why.
To fix this, locate where matplotlib is installed in your computer, and verify the folder is part of your sys.path.
import sys
sys.path
['C:\\Python27\\tests', ..., ...]
Then you've two options: insert that path from your script, i.e adding a line such import sys; sys.path.append(<folder>) or configure PYTHONPATH env variable under your user appending the folder to the path.
PYTHONPATH env variable gets loaded to sys.path on startup.
Best solution to me is a general workflow for all projects: use virtualenviroment]1.
sudo pip3 install virtualenv
virtualenv myenv
source mynenv/bin/activate
Then you should install your libraries again with pip and they will be installed in your virtualenviroment, isolated from everything else.

Running `pip` in the Google Cloud Shell causes ImportError

When I create a new project in Google Cloud Console and just try to run
pip
I get this error:
Traceback (most recent call last):
File "/usr/local/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
File "/home/sguergachi/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 70, in <module>
import packaging.version
ImportError: No module named packaging.version
I'm not sure how to fix this. Reinstalling doesn't seem to be an option for google cloud as it would for local environment.
It seems to be able to do anything in pip on GCP you need to run as sudo.
sudo pip
This fixed my issue.
First, pip and python are for Python 2; pip3 and python3 are for Python 3. Prefer Python 3 as most apps are finally getting away from Python 2.
Install Python apps in your $HOME folder, which persists across your GCP time/space continuum:
pip3 install --user <app>
Note you'll want to add ~/.local/bin to your PATH environment variable, discussed at length in https://unix.stackexchange.com/a/26059/46798, by adding the following to ~/.profile:
# add local Python apps to PATH
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi

django wont show up in virtualenv

I am trying to use virtualenv.
I installed it on my ubuntu system followed simple steps given here
but when I activate my virtual environment and try to do
$ python manage.py runserver
it throws error
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
I tried to see if django is accessible from my virtual env so it tried to
$ python
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
and the same import django works fine out of the virtualenv.
I also tried to delete the django folder from root python setup and reinstalling it. but all in vain, the problem is still the same. What should I do now ?
PS: I tried to run easy_install Django & easy_install django gazillion times from the virtual env everytime it says django is already their.
PPS: I dont know if it is of importance but their was a django installation present prior doing all this in the system.
Check your manage.py, the first line needs to define the python executable, usually #!/usr/bin/env python. This should be the path to the python executable in your virtualenv and not to the global executable. If you had installed Django system wide this wouldn't be a problem.
Just in case
virtualenv venv
source venv/bin/activate
python manage.py runserver
[Adding this info for Windows users who hit the same problem]
If you're running into this problem on Windows then make sure that your virtual env is active (your prompt will be something like (venv) d:\myproject) and that you're running python.exe explicitly.
On the latter point, do not run py.exe (the Python Launcher introduced in 3.3) or rely on a .py file association to run Python. Both of those will run a version of Python outside of your virtual env and therefore will have the wrong site-packages.
If your virtual env has django 1.8.6 but your core Python installation does not have django, then:
python -c "import django;print(django.get_version())" will work and print 1.8.6
py -c "import django;print(django.get_version())" will fail with "ImportError: No module named django"

Categories