Python venv not creating virtual environment - django

I'm trying to create a virtual environment for my current Django project using
python3 -m venv env
however the command doesn't create any directory with bin/include/lib folders.
What exactly am I missing here?

I have a Windows 10 machine and had a same problem. It was because I had multiple versions of python. Unknowingly windows had created a python.exe in the WindowsApps folder -
Then the solution is sometimes:(there is a huge chance that, the old %PATH% got renamed)
py -m venv venv
This python.exe had a size of 0 kb, so I deleted the python.exe in the WindowsApps folder, opened a new Command prompt and it started working.

Sometime system's path environment is not aware of virtualenv.exe
solution:
install virtualenv
pip install virtualenv
run command in the directory where you want virtual environment :
python3 -m virtualenv venv

For anyone facing this issue now, simply changing the command to start python instead of python3 fixes this

why do you have to write python3 -m venv env when you base is installed as python3.6 itself?
Just do pip install virtualenv this should install virtualenv package if not already installed, then
virtualenv envname this will run and should give you a message like this, I have created a env called testenv:
C:\Users\Admin\python_projects\venvs>virtualenv testenv
Using base prefix 'c:\\python37'
New python executable in C:\Users\Admin\python_projects\venvs\testenv\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
If you get this, it is a success, else do let us know what you get, after this you must cd into the Scripts folder and then run activate

I was having this same problem. I was able to get venv working by uninstalling Python and reinstalling it (I'm using the Anaconda distribution). The py -m venv test command still doesn't have any output after running it, but now it creates a folder for me and I can activate the test environment.

Try this (works for me)
python -m venv C:\<optional-EXISTING-directory-path>\<VENV-name-u-want-2-use>
For more info: https://docs.python.org/3/library/venv.html

Install and create through:
pip install virtualenv
virtualenv <your_virtualenv_name>
Then activate the environment, by going to ./your_virtualenv_name/Scripts folder and then run:
activate

I guess I am a bit late to answer the question, but before creating a virtual environment always check if we already have a .venv hidden folder
Navigate to the directory where we want to create a virtual environment
Check if we already have one by ls -la this command will show us the hidden folders, as .venv will be hidden by default
If we do not have the .venv folder (name is up to us), then create one by mkdir .venv to follow the best convention, in this folder we can create multiple virtual environments
Now we can create a virtual environment by python3 -m venv ./venv/drf
In above folder we have created, inside that we are creating one more folder drf (Django Rest Rramework)
At last to run our virtual environment use source .venv/drf/bin/activate by this command we are running the script which is there in bin folder
I hope I was able to explain, as I am also learning
Please feel free to edit or make any changes in the post, If something is wrong

pip install virtualenvwrapper-win
try to install it and do it again

I just had the similar problem, and I realized changing the path directories names by removing the spaces in the name of the directories helps.

Related

Django Error on django-admin startproject

I try this command:
$ virtualenv .venv
$ sourse .venv/bin/activate
(.venv) $ pip3 install django
The last command installed the django3 on the whole system while the virtual machine was active and should only be installed on the virtual machine. why??
I tried django-admin stratproject mysite, but I received this error:
Command 'django-admin' not found, but can be installed with:
sudo apt install python-django-common
so, I try sudo apt install python-django-common. Then again try django-admin stratproject mysite and resived error: Cannot find installed version of python-django or python3-django.
how I can solve this problem??
Did you add a Python path. If you have, you can enter the Python terminal when you write Python. You can see the installed libraries by typing pip freeze.
The Django is not added in your python path , if you still want to run your code go to python directory and then run the django-admin command.
Go to the folder where your python is installed and open terminal there
after that run your command
django-admin startproject test
Try using pipinstall django instead of pip3 install django in virtualenv.
By running virtualenv .venv you have created local python environment with current default system version of python (i.e. on older distributions this might be python 2.7)
While activating virtual environment, it replaces some ENV variables to allow python to address this local environment, but not system one.
pip is one of them. pip inside virtualenv points to virtualenv's pip. Virtualenv might not redefine pip3 command - this is only needed system-wide, cause you may have many different versions of python in the system at the same time. And as far as virtualenv has only one version of python / packages inside - where is no need for it to redefine pip3.
Running django-admin startproject test inside virtualenv uses virtualenv's python packages, and if there is none, it may actually use system-wide packages, but it depends on the options virtualenv was created with (use system pacakges), PATH variables etc.
Also, try not to install or rely on system-wide packages - use virtuelnv.
Using virtualenv is good, and there is a lot of helpers to ease - pipenv, virtualenwrapper etc
Just run this in the command line.
apt-get install python3-django
Hope this will solve your problem.

ImportError: Couldn't import Django

I've already configured virtualenv in pycharm, when using the python manage.py command, this is error shown:
E:\video course\Python\code\web_worker\MxOnline>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
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?
How should I fix it, I've installed django.
I think the best way to use django is with virtualenv it's safe and you can install many apps in virtualenv which does not affect any outer space of the system
vitualenv uses the default version of python which is same as in your system
to install virtualenv
sudo pip install virtualenv
or for python3
sudo pip3 install virtualenv
and then in your dir
mkdir ~/newproject
cd ~/newproject
Now, create a virtual environment within the project directory by typing
virtualenv newenv
To install packages into the isolated environment, you must activate it by typing:
source newenv/bin/activate
now install here with
pip install django
You can verify the installation by typing:
django-admin --version
To leave your virtual environment, you need to issue the deactivate command from anywhere on the system:
deactivate
When you install Django on your computer all things go fine but when you install a Virtual environment it gets separated from all things. You will know it's importance when you will make a final project and deploy it to any cloud or hosting.
Just reinstall Django in the virtual environment and baam:
pip install Django
and then just run the command for testing:
python manage.py runsever
and you are all done.
You need to install Django, this error is giving because django is not installed.
pip install django
You need to use both commands:
pip install django and pip3 install django
that worked for me
Check that you have installed Django; by executing import django in python.
you mustn't see ModuleNotFoundError if everything's ok.
Check that you have installed virtualenv; by executing virtualenv --version.
you must see the version number if everything's ok.
Check that you have enabled virtualenv; there's got to be the name of your virtualenv in your command prompt starting line. enable it by
source bin/activate. also, remember to deactivate it every time your job is
finished with the virtualenv.
Check that your virtualenv includes django. a virtualenv by default
has no modules installed. you either have to install django in your
virtualenv (even if you have it in your machine already) or use
virtualenv --system-site-packages when creating a virtualenv to
include system site packages in the virtualenv.
Add django to your path. open python, import django, then run
django to see django's path. then add it to your ~/.bashrc (or
~/.zshrc if you're using zsh). more info in here
Install django-admin by running pip install django-admin
find your django parent dir path and add it to PYTHONPATH
In my case, my django parent dir path is /Library/Python/3.7/site-packages,add this line into ~/.bash_profile
export PYTHONPATH=/Library/Python/3.7/site-packages
else if you have PYTHONPATH already, just append it like this
export PYTHONPATH=${PYTHONPATH}:/Library/Python/3.7/site-packages
then
source ~/.bash_profile
I was having great difficulties with this but I have solved my issue. I am on Windows 10 using Vagrant ssh in my virtualenv environment, the box I have installed is ubuntu/xenial64, Django version 2.1, python==3.6.
When I was installing packages I was using pip3 but most importantly I was using sudo and the -H flag to install these packages. When I ran sudo pip3 freeze my packages would come up, but when I ran a plain pip3 freeze there would be no packages.
Then I tried the python3 manage.py startapp <YOUR APP NAME> and it did not work same error as you.
I finally thought to try sudo python3 manage.py startapp <YOUR APP NAME> it finally worked!
Hope this was help :)
I faced the same issue, and in my case it was because I had multiple python versions on my machine, in addition to the Anaconda ones.
In my case django didn't worked well with my anaconda python.
I knew that when I run import django on each python terminal for all versions I have.
As a summary here are the steps I made to get this solved:
Run the CMD as Admin
Create a project folder.
Create a new ENV for this new project INSIDE THE PROJECT Folder...
pip install virtualenv >> virtualenv new_env`
Activate it:
.\new_env\Scripts\activate`
After the env activation ⇒ Install Django:
python -m pip install Django
The python version you used here in step 5 will determine which python will to work with this installed Django.
If you are working on a machine where it doesn't have permissions to all the files and moreover you have two versions such as default 2.7 & latest 3.6 then while running the command use the python version with the command. If the latest python is installed with sudo then run the command with sudo.
exp:
sudo python3.6 manage.py runserver
after activating virtual env that error raises up on ubuntu.
and I solve this issue just by typing again :
pip3 install Django
inside the directory which is I want to create a new app.
You can use python3 to run file, if you don't want to use virtualenv.python3 manage.py runserver
To install python3 look at this page
Make sure you have Django installed by writing this command :
python -m django --version
if it's not installed you can install it by writing this command :
pip install django
I solved this problem in a completely different way.
Package installer = Conda (Miniconda)
List of available envs = base, djenv(Django environment created for keeping project related modules).
When I was using the command line to activate the djenv using conda activate djenv, the base environment was already activated. I did not notice that and when djenv was activated, (djenv) was being displayed at the beginning of the prompt on the command line. When i tired executing , python manage.py migrate, this happened.
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?
I deactivated the current environment, i.e conda deactivate. This deactivated djenv. Then, i deactivated the base environment.
After that, I again activated djenv. And the command worked like a charm!!
If someone is facing a similar issue, I hope you should consider trying this as well. Maybe it helps.
Instead of creating a new virtual environment, you just have to access to your initially created virtual environment when you started the project.
You just have to do the following in your command line:
1)pipenv shell to access the backend virtual environment that you have initially created.
2) Then, python manage.py runserver
Let me know if it works for you or not.
To create a virtual environment for your project, open a new command prompt, navigate to the folder where you want to create your project and then enter the following:
py -m venv project-name
This will create a folder called ‘project-name’ if it does not already exist and setup the virtual environment. To activate the environment, run:
project-name\Scripts\activate.bat**
The virtual environment will be activated and you’ll see “(project-name)” next to the command prompt to designate that. Each time you start a new command prompt, you’ll need to activate the environment again.
Install Django
Django can be installed easily using pip within your virtual environment.
In the command prompt, ensure your virtual environment is active, and execute the following command:
py -m pip install Django
In case you have virtual env activated, django installed, django-admin --version prints the valid version - check if there is no circular import in the file you are executing.
I faced the same problem when I was doing it on windows 10. The problem could be that the path is not defined for manage.py in the environment variables. I did the following steps and it worked out for me!
Go to Start menu and search for manage.py.
Right click on it and select "copy full path".
Go to your "My Computer" or "This PC".
Right click and select "Properties".
Select Advanced settings.
Select "Environment Variables."
In the lower window, find "Path", click on it and click edit.
Finally, click on "Add New".
Paste the copied path with CTRL-V.
Click OK and then restart you CMD with Administrator privileges.
I really hope it works!
Looks like you have not activated your virtualenv when using the runserver command.
Windows: <virtualenv dir>\Scripts\activate.bat
Linux: source <virtualenv dir>\bin\activate
You should see (name of virtualenv) as a prefix to your current directory:
(virtualenv) E:\video course\Python\code\web_worker\MxOnline>python manage.py runserver
windows :
(virtualenv dir)\Scripts\activate # this step to activate virtualenv
you should be in the dir of (project name)
python manage.py runserver
you need to go to the root directory
and run the below command
source bin/activate
Once the above command is executed, you will be able to create custom apps
I also face the same problem in windows 10 with anaconda
For me anaconda3\Scripts>activate
it's working good. What you have to do you just need to go to anaconda home
AppData\Local\Continuum\anaconda3\Scripts
and you need to open a cmd prompt and type activate.
It will activate the venv for you.
if you don't want to deactivate or activate the already installed venv just ensure you have set the pythonpath set
set pythonpath=C:\software\venv\include;C:\software\venv\lib;C:\software\venv\scripts;C:\software\venv\tcl;C:\software\venv\Lib\site-packages;
and then execute
"%pythonpath%" %venvpath%Scripts\mytestsite\manage.py runserver "%ipaddress%":8000
The problem is related to this error: Execution Policy Change
Start virtualenv by running the following command:
Command Line
C: \ Users \ Name \ yourdjangofilesname > myvenv \ Scripts \ activate
NOTE: On Windows 10, you may receive an error by Windows PowerShell that the implementation of these scenarios is disabled on this system. In this case, open another Windows PowerShell with the "Run as Administrator" option. After that, try typing the following commands before starting your virtual environment:
C:\WINDOWS\system32> set-executionpolicy remotesigned
Execution Policy Change:
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at http://go.microsoft.com/fwlink/?LinkID=135170.
Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): A
After selection Y(es), close the Powershell admin window, and then go back to the Powershell Window(where you got the error) and run the command again.
> myenv\Scripts\activate and then python manage.py runserver 8085 ,
(8085 or any number if you want to change its default port to work on otherwise you dont need to point out anything. )
I had same problem, I installed all dependencies with root access :
In your case:
sudo pip install django
In my case, I had all dependencies in requirements.txt, So:
sudo pip3 install -r requirements.txt
Just sync your pipenv environment with:
pipenv sync
I had this problem with Django 3.
On manage.py detail the execute_from_command_line import.
You should have:
from django.core.management import execute_from_command_line
Instead of
from django import execute_from_command_line
I had the same problem and my solution was not posted here:
How I got the error
My error came whenever I was installation the requirements.txt file with pip (let's say from cloning a git repository).
Solution
I manually installed each of the modules in the requirements.txt + any other module needed for the installation of those modules (e.g: I got errors and some modules where missing to install other modules so I had to add them too).
If there is anyone who faced with the same problem when using virtual environment and running on MacOS, just try
sudo python manage.py startapp <project_name>
instead of
python manage.py startapp <project_name>
It will solve the problem suprisingly!
I had to install django using the virtual environment pip3 executable directly:
cd [virtual environment folder]/bin
sudo ./pip3 install django
If you already installed Django / configured virtualenv and you still having the error:
ImportError: Couldn't import Django. Are you sure it's installed and
available on your PYTHONPATH environment variable?
Try to run the command pipenv shell before start the server with py manage.py runserver

Django not installed in venv?

Would anyone know possible reasons why Django is being installed in the global site package and not my venv's site package folder?
Here's my set up and what I did, this is a bit detailed since I'm new to Python/Django and not sure which information is important:
Python 3.3 is installed in c:\python33
I have virtualenv, pip, easy_install installed in C:\Python33\Scripts.
My venv is c:\users\username\projects\projB
This venv was created using pyvenv, not virtualenv.
I activated the venv.
I changed directory to C:\Python33\Scripts to run "pip install django".
Django was created inside C:\Python33\Lib\site-packages and not inside C:\users\username\projects\projB\Lib\site-packages.
Do I need to install pip inside my venv and use that to install Django?
You can specify in your virutalenv wich python version you whant:
$ virtualenv -p <PATH TO PYTHON VERSION> my_virtualenv
Then:
$ source my_virtualenv/bin/activate
$ pip install Django==1.5.2
This will install the good version of django in your virtualenv according to your python version.
Thanks to virtualanv, you will be able to save/freeze and install your environement on another machine:
$ pip freeze > requirement.txt
$ pip install -r requirement.txt
You will see in the requirement.txt file the django dependency.
Pip should be installed when you create the virtual environment. Don't change directory into C:\Python33\Scripts before running pip. It looks like that means you use the base install's pip instead of your virtual environment's pip.
You should be able to run pip from any other directory. However I'm not familiar with python on Windows, so I'm not certain that pip is added to the path when you activate the environment. If that doesn't work, you'll have to change directory into the bin directory of your virtual environment, then run pip.
What happened to me was that I was trying to install django from outside the environment directory/folder.
So make sure you are inside the environment directory and then use pip install django

VirtualEnv is installed, but cannot find bin/activate

We had a working project with git, and our instructor moved our project over to mercurial after making some changes.
I pulled the new project and started it up under a new folder.
I tried running a virtualenv for the new project but I get: Requirement already satisfied
I would usually then run $ . bin/activate but I cannot find a bin folder.
How do I get a virtualenv setup for this project at this point?
The place where you have your project isn't necessarily the place where you have the environment. Did you have the environment in git too?
Perhaps what you need to do (in case you don't have a versioned environment) is recreate the environment (virtualenv environment), install the dependencies (pip install -r req.txt) and then activate it (source path/to/environment/bin/activate).
Good luck.
Ok
Try this an awesome link to How to install django in virtual environment .
http://ayarshabeer.com/post/50973941605/install-multiple-django-version-using-virtualenvwrapper
Use the following steps
virtualenv virenv_name --no-site-packages - create new virtual environment in git
pip install -r requirements.pip - It can be used for install tools in
(requirements.pip files)
yolk -l - It will used for list install files
source git/virenv_name/bin/activate - Activate virtualenv
deactivate - Deactivate virtualenv

How to install image processing just for a virtualenv project with pip for django

But I'm looking to install freetype, libjpeg, PIL build to add image processing to my django projects I've followed this installation http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/ which installs it site wide but I can get it inside my virtualenv project.
Do I just cd into the working directory of the virtualenv (project) and install it there and will it just be available for that project or do I use pip? I couldn't find the packages in the pip repository. Can someone enlighten me please.
curl -O http://pypi.python.org/packages/source/d/distribute/distribute-0.6.21.tar.gz
tar -xzvf distribute-0.6.21.tar.gz
cd distribute-0.6.21
python distribute_setup.py
easy_install pip
pip install virtualenv
virtualenv --distribute --no-site-packages [myproject]
cd [myproject]
source bin/activate (this activates the sandbox that virtualenv created)
pip install django mysql-python
Go to the working directory of the virtualenv and then run
$ source bin/activate
This will set that virtual environment as your active one. So now that it's active, you can install what you want, either manually (by following those steps on the site you linked to) or with pip and it will automatically install it into your active virtualenv.
If you then, say, run python manage.py runserver while the same virtualenv is active, django will have access to your newly installed package. Once you want to unset that virtual environment as your active one, simply do deactivate.
I ran into something similar; what I did was install it into the default directory (e.g. Python27/Lib/site-packages) then cut and paste all the new files put there into the created environment's site-packages. Hacky, but works.
After that you can follow EEVIAC's instructions to actually get your server running.