Using Windows to run a virtual environment Created on Ubuntu - django

so i have been developing a website with a backend database. The following is my current setup and it is working great:
Currently using Ubuntu 16.04
I created a virtualenv and downloaded Django and postgreSQL within the virtual environment.
I also downloaded and am using Python 3.5.2 within the virtual environment.
My entire folder structure is on GitHub so that I can edit the code on the go (Again, everything working fine on Ubuntu).
The problem comes when I want to start doing some editing on Windows 10 using Powershell. I am unsure of how to run the 'activate.sh', 'activate.csh', or 'activate.fish' file in order to run the virtual environment and initialize my server using 'python manage.py runserver' so I can start editing my website.
Has anyone run into the problem and found out how to fix this? Any help on how to get started working on Windows would be great.
If you need any more details id be glad to provide them.
Thanks!

Assuming that you have created a virtualenv on ubuntu without relocatable option. You will have to firstly create a new virtual environment on Windows because they have differences on OS variations. So navigate to the directory where you would like to create the new virtual environment on Windows and run the following command:-
virtualenv .
(Note the . specifies current directory option)
After this there will be three directories created in your directory namely
1) Include
2) Lib
3) Scripts
As now the activate.bat file is in Scripts you can activate your virtualenv by the following command:
Scripts\activate
After the environment is activated you can pip install -r requirements.txt and then run your manage.py script as usual.
For further reference you can read:-
https://virtualenv.pypa.io/en/stable/userguide/

Related

running 'heroku login' won't create _netrc file

I am using on a 64-bit system running Windows 11 with Python 3.7 installed and working in a virtual environment where I have installed Django 3.2. I am trying to deploy my project using Heroku.
I have tried adding heroku cli 64-bit Windows version to various paths. I have set the env variable HOME with a value of C:/Users/<username>/_netrc. I have cleaned up the path on each installation so there is only the current path.
When I run heroku login from within my project, I get the following error:
EPERM: operation not permitted, open C:/Users/<username>/_netrc
Any help here is appreciated
I have set the env variable HOME with a value of C:/Users/<username>/_netrc
HOME should be a directory. Heroku will take care of the _netrc part.
Try setting it to C:/Users/<username>/ instead.
delete the existing file in path C:/User//_netrc
used git bash or terminal for it as i was unable to find the _netrc file in file Explorer.
after deleting It run the below command in project folder.
heroku git:remote -a <heroku_app_name>
by running the above command it will create a new _netrc file

What is the meaning of source bin/activate in command prompt?

I am newbie of django , I have installed django in a given folder "Dev->django" but I am not able to start django. When I run a command source bin/activate output will be like (django) akriti#akriti:~/Dev/django$
what is the meaning of (django) here?
It's the name of your virtual environment. In order to incapsulate installed packages and prevent from conflicts between different version packages for different projects it's convenient to use Virtual Environment. You can read more there.

How to start an existing Django project?

I am a new to Django and have 0 experience with this framework.
I cloned a project from git it came with requirements.txt file, but I am not sure how to run it.
Do I need to create virtual environment first and clone the project into the directory of the virtual environment, and then install the requirements?
Do I need to clone the project first into some folder and then create the virtual environment inside this folder and then install the requirements?
Do I need to use any special IDE to run the project? I tried opening the project in PyCharm, without creating a virtual environment first, and it asked me if I want to install the requirements.
I would be glad if someone could explain what is the correct way to run an already existing project.
Let's come to your doubts one by one :
Cloning a repo has nothing to do with Django. You are just making a copy of the code on your disk. Now the copy can be kept anywhere you like(say in ~/Desktop). Also, the directory of a virtual envt. just contains the code of modules you might directly import(like Django), and has nothing to do with the code of the django project. So cloning can be done before or after activating the virtual envt.
You need not create a virtual envt., but you should. A virtual envt. just ensures that you can have different versions of the same module for your different projects. It keeps things organised. So, for example you can create two different virtual envt.'s one with Django=2.0 and another with Django=1.9, to test your website for the two different Django versions.
requirements.txt contains all the modules you will be needing to run the django application. So you first create a virtual envt, activate it and then in the virtual envt., install all the modules you will need. Generally do pip install -r requirements.txt
Now all the required modules are installed, To run the website on a local server( which Django will create for you ), do python manage.py runserver and open in the browser.
No, you don't need an IDE to run the django project. Editing the code on any text editor and running the server from terminal works just fine.
P.S: If you are completely new to python, I would recommend using the conda python distribution. You can create new virtual envt. using conda create as well.
1.Grab a copy of the project.
git clone new_project.git
2.Create a virtual environment and install dependencies.
mkvirtualenv new_project
pip install -r requirements.txt
3.Duplicate new_project/new_project/local_settings_example.py and save as local_settings.py.
4.Enter your database settings in local_settings.py.
5.Initialize your database.
python ./manage.py syncdb
python ./manage.py migrate
6.If your app has a custom user model, you'll need to create a new superuser for the admin.
python ./manage.py createsuperuser
7.Run the development server to verify everything is working.
python ./manage.py runserver

is it possible to access already running virtualenv in windows

is it possible to access already running virtualenv in windows by external tools ? I need this to run cron like tasks by executing python manage.py dosometing of running instance of django. For this I thought I would setup windows task that will execute something like cmd.exe C:\django\manage.py dosometing but I dont understand how to do this if django is running inside virtualenv
Yes you can. The virtualenv is not like a virtual machine, it is not running. The virtualenv is a just a setup where you can have multiple python installations on a single machine. Each virtualenv lives in a separate directory so you can have different libraries installed, different versions of python etc. When you 'activate' the virtualenv you are just setting environment variables such as PATH which tell python programs which python executables and libraries to use.
So when people say 'Django is running in virtualenv' they just mean django is running.. and there's a virtualenv set up that is controlling which python installation Django is using.
If you look at the virtualenv documentation here: https://virtualenv.pypa.io/en/stable/userguide/ ...
It tells you that your activation script is located at:
\path\to\env\Scripts\activate
Where \path\to\env should be replaced by the actual location of your virtualenv.
So, if you are running in a command window, run the activation script first, then all your following commands will be in the virtualenv. If you're running from the scheduler etc, the easiest thing for you to do is, create a batch file which first runs the activation script, then your django command. This ensures that you'll always run in the correct virtualenv.
If you really only have one python installation, then technically the virtualenv isn't necessary.. and in that case you could edit your Windows environment variables as follows:
VIRTUAL_ENV="/path/to/your/virtualenv"
And then also, append to the end of your Path variable the path to your python and python scripts directories. On my system these are:
C:\Python27\Scripts;C:\python27\;
..and make sure there are no other python directories in your path.
If you open a powershell window and run env, it will show you the current settings of your environment variables. If you do this, run activate (in your virtualenv directory) and then run env again, comparing the Path (and other variables) between the two.. then you'll easily be able to see what activate did to your environment, and you can then add that to your Windows configuration.
Then when you run python myprogram.py it'll be using the virtualenv without activation.

Best practices of using virtual environment for web development with Django?

This is a Django and Python and maybe just a general web development question.
what's the difference between using virtualenv vs vagrant vs virtual box and etc... ?
I'm kinda confused as to when to use which one :/ I've been using virtual env this whole time and creating new virtual environments for different projects....
Is this the right way to do it?
One virtualenv per project?
I'm not really sure when and where vagrant comes into play...Am I supposed to set up vagrant and then use virtualenv?
This is probably a silly question but...if I were to be working on this project with other people. Would they too have to set up a virtual env? Just to collaborate?
Wouldn't it make more sense that we all work on our local machines and then push it into the main branch? I'm just kinda confused .... I feel like I'm doing it all wrong...
Thanks for the replies everybody!
Virtualenv sets up a local sandbox for you to install Python modules into.
Vagrant is an automation tool for creating Virtual Machines.
VirtualBox is a free, open source environment for running virtual machines, like those created by Vagrant.
Virtualenv is really about all you'll need to do sandboxed development on your local machine. We use Vagrant at my work to automate the creation of VMs. This way new developers coming on to a project have basically zero configuration to do in order to start working.
If you're collaborating with other devs, they don't need to do any of the above to work on your Django project, but if there's a lot of configuration involved that can't be done with pip and a requirements.txt, then you might look at Vagrant to ease some of that automation.
But you are correct in your assumption that you can all just work on a local branch and push back to the repo. Everything else is just icing.
Virtualenv is a python construct that holds a specific set of packages, separate from your system packages. The version of Python and its packages that came with your OS or that you installed separately is a "system package".
Virtualbox is totally different -- it's a VM, an entire operating system in a box.
I'm not familiar with Vagrant.
All you need is virtualenv. Create a new virtualenv for each project (they're very lightweight!) You need to do this because the whole point of virtualenv is to isolate the exact packages and versions of those packages you need for your project. Then activate the virtualenv and use pip install to install the packages you need, presumably starting with Django itself.
Once you have all the packages you need, use pip freeze > requirements.txt to create a file called requirements.txt that records all of the packages you've decided to use.
When other people collaborate on your project, they can start a virtualenv, pull your code into it, and run pip install -r requirements.txt to replicate your environment. They can even modify requirements.txt, push that back to you via your version control system, and you can run pip install -r requirements.txt yourself to modify your environment to match their changes.
This is all essential because without virtualenv, the problem of, for instance, having one project on your computer that requires Django 1.4 and one that requires Django 1.5 becomes very complicated.
Virtualenv is not an entire operating system in a box, just a python environment, so even if you are using it, you are still working on your local machine.
We use virtualenv and a Ubuntu virtual machine. Here's why:
virtualenv allows us to have isolated Python environments on a given operating system instance
Using Ubuntu dekstop in a virtual machine for our Python development mimics what it will look like when deployed on the server which is also Ubuntu. This means that we understand precisely the external OS package dependencies and configuration. You don't get this easily when you use OSX or Windows for development and Linux for deployment.
One important point is that a virtual machine is portable. You can take a snapshot and deploy it elsewhere easily. With Vagrant and Ansible combination you can automate a remote deployment.