I just ported a working django app from a windows system to ubuntu by just copying all the files to /var/www/some/dir/djangoApp. But now, when executing
python manage.py runserver 8080
I get the Error:
ImportError: no module named django
I have already installed a fresh version of django with python setup.py install to /usr/local/lib/python2.7/dist-packages/django/ and added the path to PYTHONPATH.
The linux system in not maintained by me and has numerous python versions installed.
calling >>> import django in the shell does not raise an ImportError.
I'm very confused. Please help me!
Here's the traceback from the console:
Traceback (most recent call last):
File "manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 280, in execute
translation.activate('en-us')
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 130, in activate
return _trans.activate(language)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 188, in activate
_active.value = translation(language)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 159, in _fetch
app = import_module(appname)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
ImportError: No module named django
Since you just migrated to a UNIX environment, i suggest you migrate also to the best practices on such a platform too.
Download PIP
sudo apt-get install python-pip
Download and install virtualenv to set up a separate python virtual environment for your apps. This will allow you to run different flavors of django and other software without conflicts.
sudo pip install virtualenv
Create virtual environment by running. You will get a folder called myvirtualenvironment with a bin folder and a few executables inside it.
virtualenv myvirtualenvironment --no-site-packages
In order to tell your shell that you're working with that newly created virtual environment you need to run the activate script found in /myvirtualenvironment/bin/
source myvirtualenvironment/bin/activate
Now you can install django specifically to that virtual environment.
pip install django OR pip install django==1.6 depending on what version you want to install. If you don't specify, the latest version will be installed.
Now, migrate your Django project inside of /myvirtualenvironment/ and run the the runserver command.
Sometimes there are some .pyc files in the directories and you don't get any error from the console. Trying to install Django from pip.
sudo pip install django
Best practices advise to create a requirements.txt file (From you Windows installation)
pip freeze > requirements.txt
And then create a new virutalenv to install every package
mkvirtualenv myapp
pip install -r requirements.txt
I landed on this page after getting the same error (On a site I've been actively developing just fine for months). For me #asaji's answer reminded me that I had forgotten to launch my virtual env.
After launching my Virtual Env . Scripts/activate it worked great!
It seems like quite a big job for a problem that (MIGHT) be very small.
I had this exact problem, it was working one day, and then the next day it wasn't working anymore. I'm pretty new to Linux and Django in general but knows python well so didn't really know where to look other than "Virtual environment"
I started installing virtual environments again (like some people suggest) BUT DON'T!
At least not until you tried this and think about it:
Did you install your virtual environment as a temp one(did you perhaps install it like this: "pip install pipenv"?)
if you did(like you should have done it) you will have somewhere around your current django project 2 files - pipfile & pipfile.lock
open your terminal, cd to the path of those files(same folder)
write in terminal: pipenv shell
BOOM: You just reactivated your "TEMPORARY" virtual environment and Django works exactly as it should, will and did.
Related
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 "C:\Users\emin\Desktop\fliteplan\gen_angular\arinc_424_19_db\multi\src\manage.py", line 22, in
main()
File "C:\Users\emin\Desktop\fliteplan\gen_angular\arinc_424_19_db\multi\src\manage.py", line 13, 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?
Make sure to have a virtual environment whenever saving new dependencies, most importantly django.
virtualenv env
source env/bin/activate
pip install django
// save the dependency
pip freeze > requirements.txt
*Note the syntax, virtualenv [env name] env can be any name but you would still activate it from [env name]/bin/activate
Did you active your virtual environment?
If didn't then you can follow this solution to do that
here is the link
And if still unable to install Django then check your python installation.
if python is working perfectly then try
python -m pip install django
and according to your os, you can follow this official documentation link
official documentation of Django to install
Thanks
On both MacOS 10.15.7 (Python 3.8.6) and Ubuntu 20.04 (Python 3.8.5), I have installed various virtual environments for various Python scripts I am writing (all in Git and on Github). The projects use pyproject.toml for use with flit. I created and populated the environments with modules used in the scripts as follows (where $VENVS is the directory where I keep virtual environments):
$ python3 -m venv $VENVS/csvenv
$ source $VENVS/csvenv/bin/activate
(csvenv) $ python3 -m pip install --upgrade flit
(csvenv) $ flit install -s # to install script as "editable"
HOWEVER, on both platforms the command python3 -m pip freeze gets different results, when executed in a virtual environment:
In environments created before August 17, it displays the installed modules (as it should).
In newer environments, I get:
ERROR: Exception:
Traceback (most recent call last):
File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 228, in _main
status = self.run(options, args)
File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/commands/freeze.py", line 101, in run
for line in freeze(**freeze_kwargs):
File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/operations/freeze.py", line 67, in freeze
req = FrozenRequirement.from_dist(dist)
File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/operations/freeze.py", line 257, in from_dist
req = direct_url_as_pep440_direct_reference(
File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/utils/direct_url_helpers.py", line 49, in direct_url_as_pep440_direct_reference
assert not direct_url.info.editable
AssertionError
My guess that this depends on when an environment was created is based both on directory timestamps and on examination of builds on Readthedocs, which rebuilds documentation whenever a repo is pushed.
Documentation for a project with an environment created before August 17 has continued to rebuild without interruption, while the documentation for a project with the newer environment has failed to build since August. With the August 17 commit, a requirements.txt file was added.
If I restore that requirements.txt, the RTD build fails with this message:
ERROR: Could not find a version that satisfies the requirement csv2shex==0.2 (from -r docs/requirements.txt (line 16)) (from versions: none)
ERROR: No matching distribution found for csv2shex==0.2 (from -r docs/requirements.txt (line 16))
If I exit the virtual environment with deactivate, execute python3 -m pip freeze >requirements.txt (which works outside of the virtual environment) and push, Readthedocs fails to build with the same error.
pip freeze >requirements.txt does work when I use a virtual environment created in July. However, when I clone that project on Ubuntu and re-create a virtual environment (as above), pip freeze raises the exception. It also fails if I create and populate a new virtual environment for that on MacOS. However, if I create a new requirements.txt file within the virtual environment from July, and push, the documentation builds.
The exception is raised at line 49 of pip's direct_url_helpers.py, which includes the following comment:
# pip should never reach this point for editables, since
# pip freeze inspects the editable project location to produce
# the requirement string
assert not direct_url.info.editable
I have researched this for many hours. Searches on pip freeze, AssertionError, editable project location, and the like have turned up nothing. The Stackoverflow search "pip freeze assertionerror" yields just 12 results, all but one from 2015, or before, plus one from 2019 about an issue involving Docker.
To summarize, it appears to me that:
On my MacOS and Ubuntu systems, pip freeze does not work in virtual environments created since August 17 (using python3 -m venv)
Readthedocs documentation builds succeed in the presence of a requirements.txt file built using a virtual environment older than August 17.
Readthedocs documentation builds fail in the presence of a requirements.txt file built outside of any virtual environment -- always with the message above, which refers to
However, some things do not add up:
I am fairly sure I would have created the requirements.txt file of August 17 from within the virtual environment because I never work with projects outside of the virtual environment. But how can that be unless pip freeze were working?
Can anyone out there explain? Is this a problem with python3 -m venv, flit, or something else? Am I at least correct to suspect that the failures of the RTD builds are somehow related to the failures of pip freeze?
The failure of pip freeze was indeed a bug in Pip 2.2. This bug has already been fixed for the upcoming Pip 2.3.
I'm still very green with python, pip, bash, and symlinks. My machine was working just fine, and I was able to run python CLIs from my computer without issue until I needed to run a serverless resource locally for debugging. I followed some instructions on our README to get everything going, and ever since then my machine is throwing errors trying to find python modules, that I can clearly see are still there. I've been searching for a solution that is similar to mine, but haven't found a fix yet.
I'm on a Mac running 10.14.6, using a Virtual Env that uses Python 3.7, and iTerm2 with zsh.
Here are the commands I used in the terminal that borked my local the dev environment.
$ brew install pyenv
$ pyenv install 3.7.5
$ pyenv global 3.7.5
This resulted in me not being able to to run pip install. I have since used brew to uninstall pyenv, and reinstall python and am now able to run pip install commands.
This command however, I do not understand, and have not been able to undo. I suspect it is the culprit of my python modules issues, but am not completely sure to be honest.
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
Here is the traceback from the python module error if that is helpful
File "/Users/<user>/<dir>/<repo dir>/.venv/bin/<evolv>", line 11, in <module>
load_entry_point('evolv==0.1', 'console_scripts', 'evolv')()
File "/Users/<user>/<dir>/<repo dir>/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/Users/<user>/<dir>/<repo dir>/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
return ep.load()
File "/Users/<user>/<dir>/<repo dir>/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2443, in load
return self.resolve()
File "/Users/<user>/<dir>/<repo dir>/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2449, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
ModuleNotFoundError: No module named 'experiments'
I have triple checked that I ran
pip install -r requirements.txt
And
python setup.py install
Which is required for this CLI to work. A thing to note, our README instructions were to use
python setup.py install -e .
However that threw an error that the -e flag was unknown. I am not sure if this matters. If anyone has some insight that can point me in the direction of a fix that would be most appreciated. And if someone can help me better understand the bash command I used I will be eternally grateful as I am desperately trying to understand bash.
I just installed Anaconda, in my Surface Pro 3, with Windows 10, using the provided installer for 64-bit. When I try to launch "jupyter notebook" I always get the following message:
Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation.
All rights reserved.
C:\Users\Carlos>jupyter notebook Traceback (most recent call last):
File "C:\Program
Files\Anaconda3\Scripts\jupyter-notebook-script.py", line 3, in
import notebook.notebookapp
File "C:\Program
Files\Anaconda3\lib\site-packages\notebook\notebookapp.py", l ine 32,
in
from zmq.eventloop import ioloop
File "C:\Program Files\Anaconda3\lib\site-packages\zmq__init__.py",
line 34, in
from zmq import backend
File "C:\Program
Files\Anaconda3\lib\site-packages\zmq\backend__init__.py", l ine 40,
in
reraise(*exc_info)
File "C:\Program
Files\Anaconda3\lib\site-packages\zmq\utils\sixcerpt.py", lin e 34, in
reraise
raise value
File "C:\Program
Files\Anaconda3\lib\site-packages\zmq\backend__init__.py", l ine 27,
in
_ns = select_backend(first)
File "C:\Program
Files\Anaconda3\lib\site-packages\zmq\backend\select.py", lin e 26, in
select_backend
mod = import(name, fromlist=public_api)
File "C:\Program
Files\Anaconda3\lib\site-packages\zmq\backend\cython__init__ .py",
line 6, in
from . import (constants, error, message, context,
ImportError: DLL load failed: The specified module could not be found.
I tried to uninstall/install again several times, I tried to install it just for me or for all the users in the computer, I tried to update anaconda first...with no success. Any clue?
Thanks!
It seems to be a problem with the default installation of Anaconda. So, I removed the pyzmq package, which seems to be the problematic one.
This is what I have done:
conda uninstall pyzmq (This also removes jupyter related packages!)
conda install pyzmq (to reinstall it)
conda install jupyter (to reinstall jupyter related packages)
Now I can open Jupyter Notebook!
I also got somewhat same error today on my system and now it got resolved by following solution.
Please add following in the path variable, it will be resolved thereafter (I just checked):
C:\ML\installed_tools\anaconda3 (was already present)
C:\ML\installed_tools\anaconda3\Scripts (was already present)
C:\ML\installed_tools\anaconda3\Library\bin (added now)
C:\ML\installed_tools\anaconda3\Library\mingw-w64\bin (added now)
Refer this link for exact exception which I got (with exact versions of the modules) and resolved thereafter. Above exception might be of older anaconda version but solution should be same.
Here is what worked for me (You have to upgrade pyzmq):
python -m pip install --upgrade pip
pip install --upgrade pyzmq
After that it worked fine.
There are multiple options to fix this, i am still investigating on the root cause. However, you can try the solution given below..
if the Jupyter notebook version is 5.1.0 & above, you can uninstall using << conda uninstall notebook >> and then install Jupyter notebook from Anaconda Command prompt using << conda install notebook=5.0.0 >> This will help you launch the Anaconda Navigator from base environment itself.
Second option..,
Create another environment in conda << conda env create -f {name of yml file}.yml >>.
After creating, open the Anaconda navigator UI, switch the environment to the newly created environment and launch Jupyter ( this will work even with latest Jupyter notebook version 5.3.7 as well), it will work.
I am still investigating why the latest version is not opening with the base environment. however, we can use solution 1 or 2 based on your preference.
There may be access privileges issues with installing in the \Program Files folder.
I have Anaconda3 and installed into the root C:\ directory and had no problems with dll file installation like you have.
Try installing Anaconda3 as Anaconda3 with no spaces directly as its own folder in the C:\ directory.
Once installed, you can use the Anaconda Cheat Sheet to get started quickly to verify that installation works by going into the Anaconda3 folder and (if you are on Windows) SHIFT+RIGHT-CLICK-MOUSE and it will give you menu with open to open Command [Line] Window directly in that directory of Anaconda3. Open the Command Line window and run the commands from the Anaconda Cheat Sheet to quickly get started:
http://conda.pydata.org/docs/_downloads/conda-cheatsheet.pdf
You will need to follow instructions to create virtual Python development environment/folder within the Anaconda3. Then you will need to make sure you install the Jupyter Notebook into that virtual env/folder. Of course you will need to "activate" that virtual env after installing.
I hope this helps.
Regarding the issue of opening Jupyter Notebook with Anaconda, I was having this error message:
[W 12:34:52.309 LabApp] Permission to listen on port 8984 denied.
[C 12:34:52.309 LabApp] ERROR: the notebook server could not be started because no available port could be found.
It was trying each port but failing because of no permission. Running jupyter on a custom port fixed the problem:
jupyter notebook --port 9999
I just wasn't able to start Jupyter notebook directly after the installation.
Using a new terminal window was the solution for me.
So i have been playing around with python (2.7.x) and django framework on my ubuntu (12.04). The way i setup django based project is by using virtualenv
Which i did it like this
** FROM TERMINAL **
1. sudo apt-get install python-setuptools
2. sudo easy_install virtualenv
Then i continue setting up my directory for my django project by using this command:
virtualenv --no-site-packages project-name
Soon after that, i activated my virtualenv:
source project-name/bin/activate
Then i continue with the Django framework installation using this:
sudo easy_install Django
and verify that Django framework is installed within my virtualenv by checking there is a file called django-admin.py under project-name/bin/ directory (which is exists).
However, the second time i tried to create another django-project (completely different one) following the same exact step as above, I don't have django-admin.py installed in the correct directory. It's get installed to /usr/lib/python...
And when i tried to run the app i get this message:
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
Does anyone know why is this happening?
Thanks.
* EDITED PART *
responding to sachitad's answer
i get the following message when i executed his/her suggestion
(project-name)blah#blah:~/Documents/python/project-name$ easy_install django
error: can't create or remove files in install directory
The following error occurred while trying to add or remove files in the
installation directory:
[Errno 13] Permission denied: '/home/blah/Documents/python/project-name/lib/python2.7/site-packages/test-easy-install-3775.write-test'
The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/home/blah/Documents/python/project-name/lib/python2.7/site-packages/
that's why, i initially thought i need to use the sudo command.
Problem:
sudo easy_install Django
Even if you have activated the virtualenv, while installing packages inside virtualenv, never ever use sudo. If you use sudo, it assumes you are installing on the system path(/usr/lib/local/..).
Thus,
easy_install django
OR
pip install django
should work.
As an addition to sachitad answer, I suggest you have a look at virtualenvwrapper which allow you to manage easily you virutal env with commands like :
mkvirtualenv your_project_name // create a virtual environment
workon your_project_name // select this virtual environment
pip install django // will install in this virtualenv
./home/user/path_to_virtual_env_project_/bin/pip install Django
you call pip which not in virtual env.
It seems activate not work.