How to convert an existing Django app to run in a virtualenv? - python

There are so many questions about this, and so many tries I've made and probably bungled...
But let me stick to the real problem: I have a fledgling Django app that I want to insulate from future changes to support software. I think putting in under a virtual environment is the answer. I'd like guidance, even just a pointer to the right Howto (one about migrating, not a fresh install).
My environment is Ubuntu 16.04.3 LTS, apache2, python3.5 and django 2.0. I'll be upgrading to the next LTS, which is why I want to insulate this app from changes.
Complicating matters is the fact that python2 and python 3 are both here, and pyhton2 is the default (what you get when you just call for "python". That makes things weird, for instance, because pip3 is uses the default python, so the output of 'pip3 freeze' is very different from what I get when I run it under python3, and I don't know the details of why.
What has failed in the past is my attempts to do it following guidance aimed at a freshly installed OS. What's more, probably because I did something wrong, pip3 lives in my $HOME/.local/bin/pip3. I don't know how to undo this.

If you have an existing environment that you wish to replicate inside of venv or on another machine inside of venv, then do this command inside your original environment:
pip freeze > requirements.txt
On the other machine or inside the virtual environment ( after having run path/to/venv/bin/activate ), do:
pip install -r path/to/requirements.txt
This should save and then restore your environment.

Related

Waitress installed via pip, but cannot be executed

Overall, pip is working fine on my server. I have just installed the package waitress and the installation seems successful. I checked it with pip freeze:
$ pip freeze | grep waitress
waitress==2.1.0
Waitress also can be imported via python3:
>>> import waitress
>>>
However, waitress-serve cannot be executed:
$ waitress-serve
Command 'waitress-serve' not found, but can be installed with:
apt install python3-waitress
Please ask your administrator.
I am not a root user on this server. Could this be a reason why the package was installed partially, or am I speculating here?
Since I am not authorized to run apt install and since the simple pip install worked in my virtualenv, I would like to be able to get this to work without using the suggested apt install python3-waitress command.
The conclusion is that, while it is installed both inside and outside virtualenv, it is only actually executable inside it.
When installable Python packages give you an actual entry point, generally it will not be on your path.
When you use a virtual environment, activating the environment puts various parts of that environment onto the path temporarily. This is intended to ensure that commands like python or python3 run the environment's Python, but it also allows those entry points to be found on the path.
A system Python installation (here I mean, not just a Python that comes with your operating system, but also one that you install manually after the fact - but not a virtual environment) will generally not have its library folders on the path by default - only enough to make python and pip work. (On Windows, often even these are not added to the path; instead, a program py is placed in the Windows installation folder, and it does the work of looking for Python executables.) Even if you are allowed to install things directly into a system Python (and you should normally not do this if you can avoid it, even if you're allowed to), they won't be findable there.
Of course, you could execute these things just fine by explicitly specifying their paths. However, the normally correct approach is to just ensure that, when you want to run the program, the same virtual environment is activated into which you installed the package.
(On my system, I have one main "sandbox" virtual environment that I use for all my projects - unless I am specifically testing the installation process, or testing how the code works on a different version of Python. Then I use a wrapper script to open a terminal window, navigate to a folder that contains all my projects, and activate the environment.)
if waitress is installed inside a virtual environment, I think you may have accidentally (or not) gotten out of said virtual environment.
If you are running a virtual environment, you can try the following commands, one after the other:
source venv/bin/activate #venv is assumed to be the name of the virtual environment you are using.
pip install waitress
waitress-serve
anytime you need to use waitress you will need to activate the virtual environment yet again:
source venv/bin/activate
waitress-serve
Please take note I am assuming you are running in a Linux environment
If this is not the issue you are facing, then feel free to expaund a bit more on your question.
Edit: Installing with pip and running it worked perfectly on my virtualenv; see the picture below, running on python 3.8.10

corrupted python virtual environment after update

this winter I've been working on a Flask Application following this tutorial.
Today, hence after 3 months, I decided to work again on it, but all my set up seems corrupted.
The application started simply activating the virtualenv and calling flask run.
As for today, the command returns:
No module named 'flask'
and so for pip, pip3 etc. even if all these modules are in venv/bin.
My only guess is that since then, I updated python systemwide to Python 3.8.3rc1, which somehow became the default python in the venv as well, even if I was working in python 3.7. If that is the case, I wouldn't know how to fix this problem.
Do you have any suggestion?
thanks
When you created your virtual environment (let's call it v), two things happened:
v/bin/python was a link to your system Python 3.7
v/lib/python3.7 was created for packages installed to the virtual environment.
When you updated, the v link remained the same, but now it points to Python 3.8, which will look for a library directory named python3.8. Hence, your old Flask installation is invisible to the new Python.
Ideally, you wouldn't use your system Python for anything; install your own Python (under /usr/local/ or something), so that instead of upgrading to a new version of PYthon, you can simply install a new version along side it. Then your virtual environment can continue using whatever version of Python it was created with.
That said, you should probably just recreate your virtual environment from scratch.
Try to update the Pip
->> pip install --upgrade pip
and then install the falsk again
->>pip install flask
and lemme know if it works

Can I use flask without virtual env?

I am having issues with flask, and now I am wondering if there's a way to use flask without virtual environment on Python. Why would we need virtual environment with flask?
$ sudo pip install virtualenv
$ sudo apt-get install python-virtualenv
$ virtualenv venv
$ . venv/bin/activate
$ venv\Scripts\activate
I was searching on Google and couldn't find a good answer for that! If there's a way to use flask without virtual environment could you please show me how?
Yes, you can. You can use any python library without virtualenv. What virtualenv does is create a sandbox environment for you so you can install whatever python libraries you want without affecting anything else on your computer. When you delete that virtual environment, all those libraries go away like it never happened.
That way you can have one project that uses version 1 or Flask and another project that uses version 2 and they won't step on each other in any way. It lets you segregate python projects so you don't have to worry about them interfering with each other.
It's generally recommended that you use it. In addition to the benefits already mentioned, it helps eliminate environmental issues between your development environment and other environments like production. Otherwise you can get into a situation where things work fine on your box but when you go to deploy it, there are problems. Usually that's because you were using the wrong version of a library without realizing it. The virtualenv system helps prevent that from happening by making sure your app only has access to the versions of the libraries you want it to. When you move your app to production the entire virtualenv sandbox goes with it so it's pretty likely it will work the same way as it did on your dev box.

Python Click application with setuptools integration runs into a python OS error when I try to reproduce the project's venv on other machines

I'm trying to allow other computers to also view a simple CLI application that I've put in a virtual environment. After searching for a while, I pip froze and generated a requirements.txt. When I attempt to install dependencies on a remote computer via pip install --editable. The terminal outputs a Python OS error.
The project is available at https://github.com/JonW27/calc
Screenshots of the error are provided:
I have a strong feeling that I've made a beginner's mistake/ did something dumb. If the post needs clarification then please specify, I honestly don't know what's wrong with it– I made the venv relocatable and ran into no errors.
Based on the error message, it appears you are attempting to install the package to the python's system-wide location. Try re-creating your virtualenv.

How to set a user-local python interpreter as default on clusters

I got access to the server of my lab yesterday. I was trying to do some scientific computing on it with Numpy or so.
The problem is that the server has a SuSE linux(I never heard of such thing..) installed, and it's very difficult to install anything on it. So the administrator suggested that I should set my python environment locally since I did't have root access.
I have checked the default python interpreter on the server, it has python2.6 and python3.x. But I wish to use python2.7.x.
I followed some instructions to install python2.7 into /home/kevin/.local with something like this:
./configure prefix='/home/kevin/.local'
It works, but not easy, because I have to type
/home/kevin/.local/bin/python
everytime.
So, here is my question: what should I do to set the python2.7 I have installed locally as my default python evironment, so I can use
python test.py
to run my script. And when I use pip, it will install modules locally automatically.
ps: I have went through many of the threads available out there, but they only covers how to install modules locally. So I don't think they meet my purpose. And I also tried virtualenv. As I mentioned before, the default python version is 2.6 in usr/bin/python, so I can only get a python2.6 virtual environment locally, which is not what I want.
Since English is my second language, I'm not sure whether I have got everything clear. If not, please point out. It would be very kind of you to do so. And thanks for help!
If you already have python2.7 and virtualenv installed locally, you already have everything you need.
The piece of information you're missing is that you can specify which version of python you want to use when you create your virtualenv. You can use the -p option for that.
virtualenv -p /home/kevin/.local/bin/python my_virtual_env
Once you activate it, your python command will refer to the proper installation. Also, if you install via pip, your packages will be installed in /home/kevin/.local/
Virtualenv's documentation can help you better understand if you have more issues.

Categories

Resources