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.
Related
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
I'm an experienced developer, but not very familiar with Python and Pyramid.
I'm trying to follow some (a bit old and unclear) instructions on deploying a Pyramid web application. My OS is Ubuntu 16.04.
The instructions say to install virtualenv and Pyramid - I do so with apt install virtualenv and apt install python-pyramid. Then they say I should run the app in a virtual environment, so I build that with virtualenv . -ppython3, and activate it with source bin/activate. I install the application from a ready-to-run buildout from GitHub. The buildout includes a "production.ini" file with parameters to pserve.
But Pyramid is not included in the virtual environment built with virtualenv. (There is no "pserve" in the bin directory, e.g.) So I can't run the applications with bin/pserve etc/production.ini, as the instructions say. And if I try with only "pserve", I get errors when trying to access files like "var/waitress-%(process_num)s.sock". Files that the app excepts to find in the virtual environment.
I've looked for flags to tell virtualenv to include Pyramid, but couldn't find any. Am I overlooking something? I'd be most grateful for some help! :-)
/Anders from Sweden
Perhaps you might want to try installing Pyramid in your virtual environment using pip, since apt-installed libraries are installed into /opt, rather than being visible to Python. In the guide, it seems like you're wanting to install Pyramid through the virtual environment so that it can be used by your program, so I think you'd be best using pip rather than apt-get. I did a quick Google search, and it seems like this is the library you need. Here, all you'd have to do is run the installation command once you've already entered the virtual environment with pip install pyramid. This way, you should only have access to it within the virtual environment, as well!
You mentioned it's using buildout - I assume this is zc.buildout. buildout usually manages its own virtualenv and handles installing all of the necessary dependencies. It really depends on how that buildout is configured as there's no standard there for what to do or how to run your app. I would normally expect pserve to be exposed in the bin folder, but maybe another app-specific script is exposed instead.
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.
I run Vagrant on Mac OS X. I am coding inside a virtual machine with CentOS 6, and I have the same versions of Python and Ruby in my development and production environment. I have these restrictions:
I cannot manually install. Everything must come through RPM.
I cannot use pip install and gem install to install the libraries I want as the system is managed through Puppet, and everything I add will be removed.
yum has old packages. I usually cannot find the latest versions of the libraries.
I would like to put my libraries locally in a lib directory near my scripts, and create an RPM that includes those frozen versions of dependencies. I cannot find an easy way to bundle my libraries for my scripts and push everything into my production server. I would like to know the easiest way to gather my dependencies in Python and Ruby.
I tried:
virtualenv (with --relocatable option)
PYTHONPATH
sys.path.append("lib path")
I don't know which is the right way to go. Also for ruby, is there any way to solve my problems with bundler? I see that bundler is for rails. Does it work for custom small scripts?
I like the approach in Node.JS and NPM; all packages are stored locally in node_modules. I have nodejs rpm installed, and I deploy a folder with my application on the production server. I would like to do it this way in Ruby and Python.
I don't know Node, but what you describe for NPM seems to be exactly what a virtualenv is. Once the virtualenv is activated, pip installs only within that virtualenv - so puppet won't interfere. You can write out your current list of packages to a requirements.txt file with pip freeze, and recreate the whole thing again with pip install -r requirements.txt. Ideally you would then deploy with puppet, and the deploy step would involve creating or updating the virtualenv, activating it, then running that pip command.
Maybe take a look at Docker?
With Docker you could create a image of your specific environment and deploy that.
https://www.docker.com/whatisdocker/
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.