Virtualenv and django not working in bash on windows 10 - django

I have a problem with using virtualenv and django in bash. If I type python -m venv env in cmd, then env\Scripts\activate, and then virtualenv - I get 'virtualenv' is not recognized as an internal or external command, operable program or batch file.. If I do the same in bash I get bash: virtualenv: command not found. How do I fix this?

Try the following to resolve your issue.
Check all of the environment variables related to the software you require to be used at least.
Check the permissions for files and folders for the software.
Sometimes uninstalling and installing the software with issues can solve problems quickly.
If you have performed number 2. and you are still have errors, proceed to number 3.
You may have dependencies missing, a good tool i have used on Windows is Dependency Walker, and the software will check if any file and dependencies are missing, and you should be able to download them.
An error message may output a file is not found but in fact a dependency is missing, relating to the software you are trying to run.

Try the following steps in the terminal, it may solve your problem.
using terminal, mkdir to make a directory for your project
cd to your project folder/dir
type pip3 freeze, it will show up all the installed packages and dependencies on global scope/system
but we gonna have a venv where we will install our necessary packages and dependencies
type python3 -m venv ./venv to create venv inside your current project folder, please ensure you are inside the folder before running this command
[if you are not using python 3, then the command will be python -m venv ./venv]
to actiavte environment,
on mac, run source ./venv/bin/activate ||
on windows, run .\venv\Scripts\activate.bat [if it doesn't work, try to put your absolute path]
you can check what is installed inside venv using pip freeze, you will see nothing inside the venv
Now you can install django inside venv for your project
to deactivate the environment, just type deactivate

Related

I can't create a django project

I typed django-admin startproject myproject in command pallet and it gave me this error message: 'django-admin' is not recognized as an internal or external command, operable program or batch file. I would realy appreciate some explination or tips. Thank you!
I will assume that you are using Linux. You have to follow some basic steps in order to make Django work.
First of all, verify if you have Python3 installed in your operating system:
python3 --version. It should return something like: 3.6.9.
Then, you have to install Django. I recommend using virtualenv for that. First of all, install venv and pip for Python:
sudo apt install python3-venv python3-pip.
Then, create virtual environment (so you don't break your "main" Python instance):
python3 -m venv venv.
Activate virtual environment:
source venv/bin/activate
Then, install Django in Virtual environment:
pip install django
If you followed all of the steps above, Django should create new project.
If you're using Windows, MacOS or ChromeBook - then verify, how to install Django there. In case of Windows I recommend WSL, so you can do everything in "linux way".
These methods may be helpful to you:
django-admin is not recognized as an internal or external command,operable program or batch file.
Troubleshooting “django-admin is not recognized”
django-admin' is not recognized as an internal or external command, operable program or batch file.
https://docs.djangoproject.com/en/3.0/howto/windows/

Python venv not creating virtual environment

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.

Error message 'mkvirtualenv is not recognized as an internal or external command'

While I was trying to execute the mkvirtualenv command on the command prompt, I was getting this error:
C:\Users\mukesh>mkvirtualenv myproject
'mkvirtualenv' is not recognized as an internal or external command, operable program or batch file.
For Python 3.3 or newer, Commands for installing, creating and activate virtual environment has been changed.
You can install virtual environment using pip:
py -m pip install --user virtualenv
For creating new environment:
py -m venv myproject
To activate your virtual environment:
.\myproject\Scripts\activate
After activating virtual environment, You’ll see “(myproject)” next to the command prompt.
You may find this link useful, as it shows the steps required. It is possible you have simply missed the earlier steps, leading to the error.
The below information is from: https://docs.djangoproject.com/en/2.2/howto/windows/
This will run you through the creation of a virtual environment on Windows:
Install virtualenv and virtualenvwrapper¶
virtualenv and virtualenvwrapper provide a dedicated environment for each Django project you create. While not mandatory, this is considered a best practice and will save you time in the future when you’re ready to deploy your project. Simply type:
pip install virtualenvwrapper-win
Then create a virtual environment for your project:
mkvirtualenv myproject
The virtual environment will be activated automatically, and you’ll see “(myproject)” next to the command prompt to designate that. If you start a new command prompt, you’ll need to activate the environment again using:
workon myproject
To create a virtual environment,
decide upon a directory where you want to place it, and run the venv module as a script with the directory path:
python3 -m venv tutorial-env
This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter and various supporting files.
A common directory location for a virtual environment is .venv. This name keeps the directory typically hidden in your shell and thus out of the way while giving it a name that explains why the directory exists. It also prevents clashing with .env environment variable definition files that some tooling supports.
Once you’ve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate.bat
On Unix or MacOS, run:
source tutorial-env/bin/activate
some times the environment will not active because users are not allowed to run scripts in the current system so you should
-go to windows PowerShell
-run as administrator
-then past this code
-Set-ExecutionPolicy -Scope CurrentUser Unrestricted
now you are allowed to run scripts on your system
then try this again
py -m pip install --user virtualenv
For creating new environment:
py -m venv myproject
To activate your virtual environment:
.\myproject\Scripts\activat
If you have a Windows computer (and installed the Windows version virtualenvwrapper-win), make sure you add the Scripts folder to the path. As per the installation instructions:
To use these scripts from any directory, make sure the Scripts
subdirectory of Python is in your PATH. For example, if python is
installed in C:\Python27, you should make sure C:\Python27\Scripts is
in your PATH.
Here's some decent instructions on how to edit your path. Nowadays you can probably create a new entry after selecting to edit the path environment variable. That new entry should just be the location of the Scripts folder (including the Scripts folder). No need to add semicolons to a super long path name - it generally does that for you nowadays. You'll probably have to restart your computer for it to take effect.
You can find out where your Python is installed here. If you are on Windows and installed Python via the Microsoft Store, you won't see a Scripts folder. In that case, install Python from the Python website, not from the Microsoft Store.

unable to complete django installation

I downloaded the necessary files from the link https://github.com/django/django.git and pasted them in to my virtual env directory
After setting up and activating the virtualenv, when I run the following command:
$ pip install -e django/
It produces this error:
(ENV) C:\WINDOWS\system32>pip install -e django/ django/ should either
be a path to a local project or a VCS url beginning with svn+, git+,
hg+, or bzr+
I am a Windows user. I think the command is for bash not for cmd.
Is it necessary to use this git tool to finally work with django ?
As instructed on the Django website :
If you're just starting out with Django development I'd recommend looking at some YouTube videos before jumping into the Django docs. Personally when I was starting out I found that the docs were quite hard to understand in the beginning, but as you get better you can refer back to them more and more.
Here's a good beginner video series to get you started.
In any case, I would recommend using virtualenvwrapper-win so that you can work on multiple Django projects without any conflicts.
First, ensure that you have added Python to the Windows environment. Open CMD and run pip install virtualenvwrapper-win.
Then cd to whichever directory your project files will be in and run mkvirtualenv projectname.
Finally run setprojectdir path/to/folder
Now whenever you want to enter that virtual environment and work on your project all you have to do is run the command workon projectname and it'll do the rest for you. You'll know it worked if on each new line in the command prompt it gives you (projectname) in brackets.
To actually install Django all you need to run is pip install django while in the virtual environment.
From your question, I suppose that you are trying to install django inside your virtual directory. If that is correct you dont need to get it from git.
Alternate way is to create a directory "main" and then project directory "mydjangoproject" inside it and a virtual environment "env".
C:\>mkdir main
C:\>cd main
C:\main>mkdir mydjangoproject
C:\main>virtualenv env
Now activate the virtual environment.
C:\main>env\Scripts\activate
Then install all the package in it. e.g
(env) C:\main>pip install django

how to bring out django project from virtualenv

I have django project in virtualenv and now I am publishing it in server but the problem is I can not move project from virtualenv, when I do this then related packages inside site-package, cant be read and errors occur, how can I bring out my project from virtualenv without any issuing
Create a new virtualenv on the server. It's easy
Step 1 Get the list of modules in the current virtualenv
source /path/to/current/bin/activate
pip freeze > /tmp/requirements.txt
Step 2 Create a new virtualenv. Login to your new server, copy the requirements file there. Then either change into a suitable directory before excuting the virtualenv command or give a full path.
deactivate
virtualenv -p python envname
Step 3 Install modules
source envname/bin/activate
pip install -r /tmp/requirements.txt
That's it.
As #bruno has pointed out, you really should be using a virtualenv on the server. And you should be using it on your local dev server as well. Then you can be really sure that the code will run at both ends without any surprises.