How can I configure Django setup to not need sudo? - python

I have pip, virtualenv, and django installed globally. Using py3, default is set using alias line in ~./bash_profile - so py2 packaged with Mac still there.
In new virtualenv, activated, but when I try to do anything with django get following error:
$ python manage.py migrate
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'
If I run with sudo (i.e. sudo python manage.py migrate) command works.
I know problem is likely how I installed pip, but anyway to fix this without re-installing everything?

Instead of python manage.py migrate, simply tell it python. It will start an interactive python interpreter. Tell it the following:
import sys
sys.executable
sys.path
sys.executable is the full pathname of the Python executable. It should be something inside your virtualenv. sys.path is the list of directories in which Python searches for modules whenever you try to import something. This should include your virtualenv.
Now exit Python and tell it sudo python. Enter the same directives. What are the differences?
This should give you a hint on what you've done wrong and you should be able to debug it further.
You might also find my article, virtualenv demystified, useful.

This can happen for several different reasons (from most basic to more complicated):
Django not installed, either globally, in your virtualenv, or both. Check using pip list. Most of the time pip install django will fix. Do not sudo this, or you will have to sudo everything. Also: double check django version matches python version; pip install --upgrade django to get latest version.
Virtualenv not activated - use source your_path/venv/bin/activate
Mac users - running python3 on virtualenv, but python2.7 default on comp. Fix by deactivating first virtualenv, manually deleting bin||venv, start new virtualenv set to use python2.7. Use virtualenv -p /usr/bin/python2.7 env.
Similar - server running different versions. Sometimes you can fix with alias (ala alias python=python2.7), but make sure to only use on instance being used for project, not on local comp. Local comp wont work, because sudo being run for default python version regardless.
In manage.py defaulted path not correct. This depends on your setup and where python dependencies stored. If this is your problem, replace shebang with #!/usr/bin/env python. Be careful re: python versions here, so if issue with OS default different from using, just add number to end of python (ex: python3).
Follow #antonis answer, figure out where in $PYTHONPATH not matching up with sudo PATH. Pipe/update profile to fix.

Related

How to fix 'no module named requests'

I have requests module installed but it shows an error when running .py file from cmd prompt.
There are no errors when running the file from vscode.
It seems that requests is installed in your virtual enviroment "my_env" but you must execute your script using the intepreter of your virtual enviroment (It seems that windows keep using the base interpreter even if you activated the virtual env). Try calling it directly, for example
"Your_path_to_venv\Scripts\python.exe" main.py
This issue can be caused by multiple issues.
Wrong pip version is in path (Caused by installing a newer version of python, while an older version was already there.
This can be fixed by simply removing the old python from PATH and restarting the command line.
Try using pip3 instead of pip maybe pip installs libraries in a different dictionary than pip3 does
If that didn't work then use
python -m pip install “your library”
And if you were using python3 just do the same
python3 -m pip install ”your library”
If they did not work too, replace pip in the last two commands with pip3
And if it still does not work first see the path of the python site-packages files by running this python code
import os
import inspect
print(os.path.dirname(inspect.getfile(inspect))+"/site-packages")
a path will be printed, take it and add it as a parameter to your pip command
pip install -t “the printed path”
Ok, I see that you probably have a script called requests.py in your C:\Users\BokaBu>pyproj\my_env\Scripts\activate folder, but your question is how can C:\Users\BokaBu>pyproj\src\main.py find it.
The solution is to add C:\Users\BokaBu>pyproj\my_env\Scripts\activate to PYTHONPATH. The solution to the general version of your question can be found here:
Importing files from different folder - Stackoverflow
But in more detail, you may want to try this to your C:\Users\BokaBu>pyproj\src\main.py:
# In C:\Users\BokaBu>pyproj\src\main.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, 'C:\Users\BokaBu>pyproj\my_env\Scripts\activate')
import requests
Hope it helps~

Unable to run python manage.py runserver command even though django is installed

I am not able to run my local server through atom terminal, even though all the requirements are meant. This is the error i get when I run python manage.py runserver,
File "manage.py", line 17
) from exc
^
SyntaxError: invalid syntax
I tried python3 manage.py runserver as suggested by some people online as a solution for mac users but it gave a different error,
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forge
t to activate a virtual environment?
Sharing the screenshot of my atom terminal.
Make sure you also installed django for python3.
By doing pip -V you can verify that your pip belongs to the python installation you expected.
You might need to use pip3 if you're running python 2 and 3 in parallel. Alternatively, you can use python3 -m pip install Django to make sure it's for python 3
Okay, this problem occurs when you use the wrong python to runserver
that is, instead of using the python you used in installing your django you used another one

Why does setting PYTHONPATH corrupt package lookup?

I have the following python file, importproblem.py:
#!/usr/bin/env python2.6
import gobject
import pygtk
pygtk.require('2.0')
import gtk
When I run it at the command line as such:
-bash-4.1$ ./importproblem.py
-bash-4.1$
It is fine. I confirm that I am using Python 2.6.6
-bash-4.1$ python --version
Python 2.6.6
When I send it to the python interpreter directly:
-bash-4.1$ python importproblem.py
-bash-4.1$
It is fine. I am trying to resolve a threading problem [my UI locks up when I start plain old threads that monitor sockets], so I built a local version of pyGTK, documented as a requirement pyGTK FAQ Archive
When I add the path to that module setting the PYTHONPATH, python then fails to find gtk:
-bash-4.1$ PYTHONPATH=/home/dscoughl/lib64/python2.6/site-packages python
importproblem.py
Traceback (most recent call last):
File "importproblem.py", line 5, in <module>
import gtk
ImportError: No module named gtk
-bash-4.1$
How do I resolve this or diagnose it further?
PYTHONPATH tells Python where to look for its libraries. If the library is not under PYTHONPATH, it is not going to find it.
A better way to handle this sort of thing is to leave PYTHONPATH as a whole pointing to the system packages, and then use virtualenv.
The default way to set this up is to make sure that pip is installed for your distribution and python version, then run
pip install virtualenv
pip may be pip2 or pip3 instead depending on your distro and its default assumptions about python.
Then to create the virtual environment for your project, go to a folder that you can relatively easily reference and execute:
virtualenv projectenv
Replace projectenv with the folder name that you want it to create the environment in.
This will create a virtual environment using your system's default Python install. If you have multiple version of python, and you want to use something other than the default, then use the following.
virtualenv projectenv -p PYTHON_EXE
or
virtualenv projectenv --PYTHON=PYTHON_EXE
PYTHON_EXE will be the name of the binary for the correct version of python. For instance on a newer system you may need to use --PYTHON=python2 in order to use version 2.6 rather than version 3.x.
To activate the virtual environment, depending on your distro and shell, use one of the following:
source /path/to/ENV/bin/activate
or
./path/to/ENV/bin/activate
Relative paths are generally ok. This will take you into the virtual execution environment.
From there you can use pip to install the packages that you want. My first step is often to upgrade pip itself within the environment.
When you are ready to stop using the virtual environment, just use:
deactivate
exiting the shell session will do the trick as well, but if you are accessing a remote system it is good to know the proper way.
Check out the documentation here for more thorough information on virtualenv, including how to use it on a Windows system: https://virtualenv.pypa.io/en/latest/
There is plenty of information on the web about how to use virtualenv, including on using it with Apache2 and Nginx for web development. Most python web frameworks will have instructions for using virtualenv.

Unable to run migrate command on command line and PIP freezes

I am using python 3 and never installed python 2 on this PC. Trying to install and use Django. I was trying to use pip to download it but when I enter pip install django==1.8 on my command line, nothing happens. No error messages, no further information. Command line just seems to freeze.
I checked if my pip was installed by running import pip on my python shell and able to do it. I ended up downloading django and installing it via a .gz file. For checking, I tried using the python shell and able to import django.
I have now created a virtual environment and activated it and my command line looks as follows now:
(myenv) c:/....... thus am in my virtual environment.
I started a new project in using django-admin startproject mysite .
The mysite folder is created alone with a manage.py file.
Next up when I try to run the command python manage.py migrate ,
I get the following error:
from django.core.management import execute_from_command_line
ImportError: No module name 'django'
My manage.py file header is correct with the following line - #!/usr/bin/env python
Am I supposed to install django again in each individual virtual environment I create? If yes how do I do it since pip is jamming up.
Am I supposed to mess with environment variable PATH for this? Can someone help please. Went through similar cases here and none is helping.
Am I supposed to mess with environment variable PATH for this?
No.
Am I supposed to install django again in each individual virtual environment I create?
Yes.
If yes how do I do it since pip is jamming up?
Even if you develop in Windows, it is really good practice to keep your environment in a linux virtual machine, and use bash scripts to prepare it, so if something goes unexpected, you can easily destroy it and spin it up again. One way of doing so is installing VirtualBox + Vagrant.
One example of bash script is here: https://github.com/torchbox/vagrant-django-template
See the Vagrantfile, and etc/install.sh. Hope that helps.

Confused about Python installations, module installation, and interpreter

So yesterday I had to create a virtualenv in order to be able to install Python modules that wouldn't install thanks to OS X El Capitan's new SIP. I thought I did everything right, but today I'm reaching a different conclusion. I hope I can be clear about it.
my python custom install is at myname/learnp/imdb_module, this is where I created it with virtualenv. Edit: I later moved it to myname/learnp/ayr2/imdb_module.
However, when I try to run the interpreter, it seems to always default to the Python that is in Library or something along these lines. I found out about this because a certain module that I managed to install in this custom python env wouldn't import, when I checked what modules I have, it wasn't the same as what I expected.
Furthermore, it seems that ALL other modules that I wanted to install on the CUSTOM virtualenv were installed on the main python env, and that I wasn't installing those modules on the custom env all along.
Excuse me, but I'm very confused right now.
I know how to create a virtual env
I know how to activate it (it appears to the right on Terminal line)
I don't know how to install modules to my virtual env
I don't know how to make the interpreter run from the virtual env so I can do python operations that are only possible by using custom env modules
Any advice is much appreciated!
Update:
Followed Will Hogan's answer for troubleshooting,and I think something weird is happening, quoting my comment to his answer:
HI, thanks for taking the time to answer. This is basically the way I understood this. However, let me attach a screenshot: http://i.imgur.com/DfpngJq.jpg . Am I right to assume something is wrong here? My prompt is changed with the virtualenv named "imdb_module", but when I type in which python it doesn't list ayr2/imdb_module/bin but rather a folder with the path usr/bin/python, which if I understand correctly is the "default" environment.
And not if this helps in any way, but echo $PATH when (imdv_module) appears to the right of the prompt, gives this (I redacted my name): /Users/REDACTEDNAME/learnp/imdb_module/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
While creating the virtualenv you should see it installing setuptools and pip:
$ virtualenv testvenv
New python executable in testvenv/bin/python2.7
Also creating executable in testvenv/bin/python
Installing setuptools, pip...done.
After ensuring the virtualenv is activated you should see your prompt change:
$ . ./testvenv/bin/activate
(testvenv)$
Now you can confirm the paths to python and pip, which should be in the virtualenv:
(testvenv)$ which python
/private/tmp/testvenv/bin/python
(testvenv)$ which pip
/private/tmp/testvenv/bin/pip
If you aren't seeing the python and pip locations as being under the virtualenv's directory, then the virtualenv has not been activated.
I would also ensure that, if you're executing the .py file directly (and not with "python foo.py"), that your shebang line uses:
#!/usr/bin/env python
Or even the full path to the virtualenv's python, e.g.:
#!/tmp/testvenv/bin/python
As opposed to, say:
#!/usr/bin/python
The first will search in the current environment, which will be set by the virtualenv activation. The second explicitly points to the virtualenv's `python'.

Categories