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.
Related
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
Environment management in Python projects is critical and is commonly done with venv or virtualenv. However, I am interested in deploying a Django project with conda.
The basic setup looks like the one here. Django is served using gunicorn which is controlled with supervisor.
That conda does not use folders as environments basically means that there is no isolated directory where the Django project could live (hope I got this right).
This post has a nice description of how to deploy a Python project with conda in general. For using this with supervisor I was thinking if activating a specific conda environment with installed dependencies in supervisor would suffice to create the needed results. I assume this supervisor config might look something like this:
[program:djangoapp]
command = .activate django-env; python gunicorn djangoapp.wsgi:application -b 127.0.0.1:8000
directory = /path/to/djangoapp
...
The supervisor docs say that supervisor will search the supervisord’s environment $PATH for the executable, if it's provided without an absolute path. Does this mean that the absolute path of the conda environment needs to be used here?
Long story short: Is anyone using conda for this and is it even usable the way I like to do it and if yes: How?
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/
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.
I'm using Hudson for the expected purpose of testing our Django application. In initial testing, I would deploy Hudson using the war method:
java -jar hudson.war
This worked great. However, we wanted to run the Hudson instance on Tomcat for stability and better flexibility for security.
However, now with Tomcat running Hudson does not seem to recognize previously-recognized Python libraries like Virtualenv. Here's an output from a test:
+ bash ./config/testsuite/hudson-build.sh
./config/testsuite/hudson-build.sh: line 5: virtualenv: command not found
./config/testsuite/hudson-build.sh: line 6: ./ve/bin/activate: No such file or directory
./config/testsuite/hudson-build.sh: line 7: pip: command not found
virtualenv and pip were both installed using sudo easy_install, where are they?
virtualenv: /usr/local/bin/virtualenv
pip: /usr/local/bin/pip
Hudson now runs under the tomcat6 user. If I su into the tomcat6 user and check for virtualenv, it recognizes it. Thus, I am at a loss as to why it doesn't recognize it there.
I tried removing the commands from a script and placing it line-by-line into the shell execute box in Hudson and still same issue.
Any ideas? Cheers.
You can configure your environment variables globally via Manage Hudson ->
Environment Variables or per machine via Machine -> Configure ->
Environment Variables (or per build with the Setenv plugin). It sounds like
you may need to set the PATH and PYTHONPATH appropriately; at least that's the
simple solution.
Edited to add: I feel as though the following is a bit of a rant, though not really directed at you or your situation. I think that you already have the right mindset here since you're using virtualenv and pip in the first place -- and it's not unreasonable for you to say, "we expect our build machines to have virtualenv and pip installed in /usr/local," and be done with it. Take the rest as you will...
While the PATH is a simple thing to set up, having different build
environments (or relying on a user's environment) is an integration "smell".
If you depend on a certain environment in your build, then you should either
verify the environment or explicitly set it up as part of the build. I put
environment setup in the build scripts rather than in Hudson.
Maybe your only assumption is that virtualenv and pip are in the PATH (because
those are good tools for managing other dependencies), but build
assumptions tend to grow and get forgotten (until you need to set up a new
machine or user). I find it useful to either have explicit checks, or refer to
explicit executable paths that are part of my defined build environment. It is
especially useful to have a explicitly defined environment when you have
legacy builds or if you depend on specific versions of your build tools.
As part of builds where I've had environment problems (especially on Windows
with cygwin), I print the environment as the first build step. (But I tend to
be a little paranoid proactive.)
I don't mean to sound so preachy, I'm just trying to share my perspective.
Just to add to Dave Bacher's comment:
If you set your path in .profile, it is most likely not executed when running tomcat. The .profile (or whatever the name is on your system) is only executed when you have a login shell. To set necessary environment variables, you have to use a different set of file. Sometimes they are called .env and they exist on global and user level. In my environment (AIX), the user level .env file can have a different name (name is set in the env variable either in global environment file (eg. /etc/environment) or by parameter, when starting the shell).
Disclaimer: This is for the IBM AIX ksh, but should be the same for ksh on other systems.
P.S. I just found a nice explanation for .profile and .env from the HP site. Notice that they speak of a login shell (!) when they speak about the execution of the .profile file.