New to linux, I have install django in virtualenv, but i am not able to import django. Is there any variable path i need to set ?
(virs) akash#akash-Inspiron-3542:/usr/virs/bin$ django-admin --version
1.11.10
(virs) akash#akash-Inspiron-3542:/usr/virs/bin$ python
Python 3.6.4 (default, Jan 28 2018, 17:52:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'django'
You need to use manage.py if you want to access django library inside python shell.
Here's a little bit example.
Try:
django-admin startproject mysite
cd mysite
./manage.py shell
or
python manage.py shell
Here's what it looks like accessing django library inside python shell. In this case, I use IPython. If you don't have IPython installed, manage.py automatically using python shell.
In [1]: import django
In [2]: django.__version__
Out[2]: '3.0'
In [3]: django.get_version()
Out[3]: '3.0'
Please run this command and write result here.
whereis django-admin
probably! django-admin command run from out of virtualenv
run the command
pip install django
Related
I've created a Python script to store data in MySQL. It works properly in PyCharm. When converted to an exe file it does not work.
This is traceback from the command line:
Traceback (most recent call last):
File "myscript.py", line 1, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
[8428] Failed to execute script 'myscript' due to unhandled exception!
The command to create the exe:
pyinstaller --onefile --console CoinVolumes.py
I also tried auto-py-to-exe but got the same error. Mysql is installed.
use this command:
pip install mysql-connector-python
after that verify:
$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
In Python 3.6.9 I can use Pip 3 and do following:
pip3 install mysql
pip3 install mysql-connector-python
You would need to run the mysql server yourself. Pyinstaller is working properly, the problem here is that mysql server is not part of python, therefore you need to start the server mannualy. An alternative is to use sqlite, which does not require a server. It is built into standard python, no need for installing anything.
I have created a virtual environment in python in ubuntu in Raspberry Pi with:
python -m venv ./venv/myname
That gives me folderstructure:
myFolder
+-- venv
+-- myApplication.py
Im activating my virtualenvironment running:
source venv/myname/bin/activate
What I dont understand is when I am running
python myApplication.py
I can run the application using python packages installed globally but not in my virtual environment. For example I can import numpy without having it installed in my virtual environment but globaly. I thought I needed to install everything within my virtual environment no matter if I have it globally or not. Do I misunderstand something here?
Im using python 3.7
This is my output from print(sys.path)
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/pi/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/local/lib/python3.7/dist-packages/Adafruit_PCA9685-1.0.1-py3.7.egg', '/usr/lib/python3/dist-packages']
>>>
UPDATE
The virtual environment seems to be running after all with testing it with os.environ['VIRTUAL_ENV']. Weird enough I can run packages I have not installed in my virtual environment. Here is output from my terminal:
(myenv) pi#raspberrypi:~/Desktop/myproject/myenv$ python
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['VIRTUAL_ENV']
'/home/pi/Desktop/myproject/myenv'
>>> exit()
(myenv) pi#raspberrypi:~/Desktop/myproject/myenv$ deactivate
pi#raspberrypi:~/Desktop/myproject/myenv$ pythonPython 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['VIRTUAL_ENV']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/os.py", line 678, in __getitem__
raise KeyError(key) from None
KeyError: 'VIRTUAL_ENV'
>>>
It is very clear to me your virtual environment (venv) has not been activated (see this SO post for ways to verify).
You can run your programs without activating by running ./venv/myname/bin/python myApplication.py. However, you probably don't want to be using this everytime as it's verbose.
There can be a ton of system-specific reasons why your venv isn't being activated after running source. My recommendation is to first reinstall virtualenv, delete your venv, and recreate.
This error has been haunting me. I have actually developed fixes and I document it and I successfully run the server on the below IP address. I try to reproduce my success after reinstalling the Ubuntu OS... with the same errors the same fixes won't do it anymore... now I need outside help. I have combed the internet for solutions. I try to add django to the PYTHONPATH and it IS added but when I exit the python interpreter the appended django path disappears!
Another item of detail that may help is that my website www.doman.com/admin is directed to the django admin login screen. I tried to log in using username and password but it fails which makes me think django is not working properly.
$ sudo python3 manage.py runserver 192.165.4.3:80
I get the error:
Traceback (most recent call last):
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?
Django is installed within my environment:
$ django-admin --version
2.0.6
Python path attempt:
$ PYTHONPATH=/home/kap/dev_env/lib/python3.6/site-packages/django python
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> sys.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> import sys
>>> sys.path
['', '/home/kap/dev_env/lib/python3.6/site-packages/django', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/kap/dev_env/lib/python3.6/site-packages']
>>> quit()
Immediately confirm django is added but it disappeared:
$ python3
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/kap/dev_env/lib/python3.6/site-packages']
sudo bypasses your virtual-environment it runs as a root user and django is not installed in root account. That is why it is throwing error .you can run without sudo or you can install django in your root account by
sudo python3 -m pip install django
Try this:
sudo python3 -m pip install django
Or if you have a requirements.txt
sudo python3 -m pip install -r requirements.txt
It is better to manage port 80 by nginx.
I actually solved it before anyone posted:
`pip install django`
is what I used. More specifically I used 'pip install django psycopg2 and gunicorn' since installing all three together seemed to fix another issue. I was using pip3 instead of pip. There are so many 'sensitivities' so that the order of installation, specific flags and package versions of installations can break or make the whole thing. I'm thinking of making a tutorial of the whole thing and putting a video online. Maybe I can set a link on this page.
Consider pipenv - git repo is here, with more information. Better control of packages, ease of deployment, and better control of virtual environments.
After installing Mac OS 10.9 (Mavericks) the Django version is no longer installed or apparently the system can't find the right paths to make it runs. How can I repair this?
This are the errors I'm getting trying to run Django:
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
>>>
$ python manage.py
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
Did you install Django into your main Python directory? If so, the version of Python might have been upgraded. This might have wiped your central site-packages. If, so, just reinstall Django.
Alternatively, and more advisable, use virtualenv. This will create a separate Python environment (including site-packages for each project), preventing this (probably) from happening again in future.
Install VirtualEnv Systemwide then setup virtual env
Installing
sudo easy_install virtualenv
now create project folder and cd into that after that
virtualenv venv
. venv/bin/activate
then install django using pip
pip install django
I have build my virtual env with this command:
virtualenv env --distribute --no-site-packages
And then I have installed several modules (django etc) into env with pip, the problem is that when I wanted to run the code on the second machine it would not work, here is what I have done:
visgean#rewitaqia:~/scripty/project_name$ source ./env/bin/activate
(env)visgean#rewitaqia:~/scripty/project_name$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.__file__
'/home/visgean/scripty/project_name/env/lib/python2.7/site-packages/django/__init__.pyc'
but when I want to run it on the second machine:
(env)user#debian:~/project_name$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
>>>
I wild error appears! The first machine is ubuntu, the second one is ubuntu. There seems to be some broken links in /home/user/project_name/env/lib/python2.7 , is that the problem? and if so, how should I prevent it/repair it?
If you are just copying the virtualenv to the second machine you may encounter some issues. From the virtualenv site:
Normally environments are tied to a specific path. That means that you
cannot move an environment around or copy it to another computer. You
can fix up an environment to make it relocatable with the command:
$ virtualenv --relocatable ENV
This will make some of the files
created by setuptools or distribute use relative paths, and will
change all the scripts to use activate_this.py instead of using the
location of the Python interpreter to select the environment.
Note: you must run this after you've installed any packages into the
environment. If you make an environment relocatable, then install a
new package, you must run virtualenv --relocatable again.
I have just noticed that I did have a wrong version of python on the second machine - debian does not have python2.7, installing 2.7 and pip for is the solution