Comprehensive beginner's virtualenv tutorial?
This is very good: http://simononsoftware.com/virtualenv-tutorial-part-2/
And this is a slightly more practical one: https://web.archive.org/web/20160404222648/https://iamzed.com/2009/05/07/a-primer-on-virtualenv/
Creating virtualenv for an existing project
You can just create an virtual enviroment with virtualenv venv
and start it with venv/bin/activate
.
You will need to reinstall all dependencies using pip, but the rest should just work fine.
What are dependencies and why should I care about them?
Virtualenvs are isolated python environments that can be created within your machine/server, and they are useful as each of them holds specific/relevant libraries for each python project/programs of various nature that you might have (be it web applications, machine learning applications, data processing microservices, IoT, etc).
For example, let's say your machine/server is hosting 2 or more python projects/programs. Each of them might require different versions of Django, MySQL-connector, etc (which can be installed via pip
). Hence, you need separate python environments for each of these projects/programs to prevent clashes.
Creating virtualenvs are simple, you can install them via pip
.
See: https://virtualenv.pypa.io/en/latest/
Thereafter, you can go about creating different virtualenv for each python project to isolate the python environments and libraries/packages needed (installed via pip
again for each environment) for each project.
Python 3.5 install pyvenv
Kesh's answer led me in the right direction.
The problem was that I didn't actually have pip installed in my venv.
It turns out, when I built python3.5 from source, I did not have the libssl-dev package. It looks like one of the dependencies of ensurepip was the python ssl package that didn't get installed because I didn't have libssl-dev.
To fix the problem, I rebuilt python 3.5 for source with the libssl-dev package installed. The rebuilt python now included the ssl package, which allowed ensurepip to install pip in my virtual environment.
importance of virtual environment setup for django with python
A virtual environment is a way for you to have multiple versions of
python on your machine without them clashing with each other, each
version can be considered as a development environment and you can
have different versions of python libraries and modules all isolated
from one anotherYes it's very important. For example without a virtualenv, if you're
working on an open source project that usesdjango 1.5
but locally on
your machine, you installeddjango 1.9
for other personal projects.
It's almost impossible for you to contribute because you'll get a lot of
errors due to the difference indjango
versions. If you decide to
downgrade todjango 1.5
then you can't work on your personal projects
anymore because they depend ondjango 1.9
.A virtualenv handles all this for you by enabling you to create seperate
virtual (development) environments that aren't tied to each other and can
be activated and deactivated easily when you're done. You can also have
different versions of pythonYou're not forced to but you should, it's as easy as:
virtualenv newenv
cd newenv
source bin/activate # The current shell uses the virtual environment
Moreover it's very important for testing, lets say you want to port
a django web app from 1.5 to 1.9, you can easily do that by creating
different virtualenv's and installing different versions of django.
it's impossible to do this without uninstalling one version (except
you want to mess withsys.path
which isn't a good idea)
Is there a way to version my python distribution?
virtualenv
+ requirements.txt
are your friend.
You can create several virtual python installs for your projects, everything containing exactly those library versions you need (Tip: pip freeze
spits out a requirements.txt with the exact library versions).
Find a good reference to virtualenv here: http://simononsoftware.com/virtualenv-tutorial/ (it's from this question Comprehensive beginner's virtualenv tutorial?).
Alternatively, if you just want to distribute your code together with libraries, PyInstaller is worth a try. You can package everything together in a static executable - you don't even have to install the software afterwards.
Related Topics
"Fire and Forget" Python Async/Await
Binary Search (Bisection) in Python
Saving a Numpy Array as an Image
Python: Pandas Merge Multiple Dataframes
How to Compare Version Numbers in Python
How to Display Pandas Dataframe of Floats Using a Format String for Columns
Convert Datetime Object to a String of Date Only in Python
How to Access Command Line Arguments
How to Get a String After a Specific Substring
How to Pass Variables Across Functions
"Python" Not Recognized as a Command
How Are Post and Get Variables Handled in Python
Calculating a Directory's Size Using Python
How to Detach Matplotlib Plots So That the Computation Can Continue