I am about to start learning Flask, and I've followed the installation instructions at http://flask.pocoo.org/docs/installation/#installation.
I've followed the virtualenv method, as opposed to the system wide installation. I just don't understand what the structure of virtualenv and how it relates to Flask.
I ran all the commands in a directory. Does this mean that virtualenv, Flask and Jinga2 are all running in that very directory only? Does that mean I will need to set up virtualenv and flask every time I start a flask project in that project's directory?
Secondly, When I navigate outside of the directory, my prompt still has venv. How can I stop that?
Virtualenv creates an isolated environment where you can install python packages without installing them globally on the system. After you run venv/bin/activate any new installed package is then inside this virtualenv (i.e. myproject/venv/lib) and if you exit the virtualenv then the system-wide python would not recognize the packages installed in virtualenv.
It doesn't matter where you install packages using pip. When you executed virtualenv venv a folder called venv is created and all the installed packages will be copied there.
Finally, in order to exit the virtual environment simply run deactivate.
Related
I'm building a rest-api using the Django Python framework. I'm using many external python packages. I have created a python virtual environment (python -m venv venv) and after activating the venv environment (venv\Scripts\activate), I installed the requests package (python -m pip install requests). Then I pushed my project to my git repo and cloned it onto another machine. When I tried to run my Django project, it asked me to install the requests package again. Why or how can I permanently install packages into my python virtual environment or someplace else where I wouldn't have to install packages again on different machines? I'm looking for a solution similar to NodeJS - npm of installing packages as all the packages are locally installed into the node_modules folder of the project and you don't have to reinstall them on different machines. Thanks
The environment itself is not shareable in the way you specify. I'd recommend to use Docker for this use-case. If you create a docker image which has the correct dependencies, then you can easily operate in the same environment on different computers. The python venv cannot be used this way.
Nevertheless, if your requirements.txt files specify package versions, then the venv you create on the two machines should be relatively similar (depending of course on other parameters like the OS, python version, etc.).
I copied a repo containing a Python Django project from my old computer to my new one via git clone. I manage my dependecies in the project via pipenv.
After successfully cloning my repo I wanted to start working on the new computer and tried to select the relevant python interpreter for my project in VS Code . However, the path was not on the list.
So first I tried the command pipenv --venv which gave me the feedback: No virtualenv has been created for this project
So I thought I might need to activate the virtual environment first, before being able to select it in VS Studio code. So i ran pipenv shell in the root directory of my project.
However this seem to have created a new virtual environment: Creating a virtualenv for this project… Pipfile: C:\path\to\my\cloned\project\Pipfile
My questions:
1.) Is this the correct way to activate a pipenv virtualenvironment on a new computer after copying the project via git clone? And if not,...
2.1) Does the way I did it cause any problems, which I should be aware of?
2.2) What would be the correct procedure to activate my virtual enviroment on a new computer?
In general an environment image probably shouldn't be copied to github. You'll get a bunch of unneeded files which clogs your repo.
Instead you should create a requirements.txt from your existing environment pip freeze > requirements.txt and commit that file.
Then when someone else clones your repo they can set up a new virtual environment using any tool they want and run python -m pip install -r requirements.txt
That is, requirements.txt is like a recipe for how to create your environment. By providing the recipe users can use it any way they want.
use:
pipenv install
It worked on Ubuntu, should work also on a mac.
I tried on a windows, it triggered some errors.
"If you download a source repository for a project that uses Pipenv for package management, all you need to do is unpack the contents of the repository into a directory and run pipenv install (no package names needed). Pipenv will read the Pipfile and Pipfile.lock files for the project, create the virtual environment, and install all of the dependencies as needed."
https://www.infoworld.com/article/3561758/how-to-manage-python-projects-with-pipenv.html
when I pip install a package it gets insalled on my macs library. I am using pycharm whih allows me to click on a package like a hyperlink. And instead of going to my site-packages in my virtualenv it's going to my macs library which is
/Library/Frameworks/Python.Framework/Versions/3.5/lib/python3.5/site-packages/gdata/youtube/
when it should be
myproject/lib/python3.5/site-packages/gdata/youtube/
why is that.
You should activate your virtual environment to install packages on that. In Pycharm you can do it like this:
Go to File > Settings > Project > Project Interpreter
Now you have to select the interpreter for this project. Browse or select the interpreter from drop-down if available. In your case this should be:
myproject/lib/python3.5
I am using Pycharm community edition on Ubuntu. But the
process should be similar in Mac.
I think you want to create a virtual environment for your project.
Install this tool virtualenv.
$ pip install virtualenv
Then create your project's folder
$ cd my_project_folder
$ virtualenv venv
This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named venv.
Source
https://github.com/pypa/virtualenv
For further knowledge read
https://realpython.com/blog/python/python-virtual-environments-a-primer/
You should install your virtual environment and then run pip within that environment. So, for example, I use Anaconda (which I thoroughly recommend if you are installing alot of scientific libraries).
To activate the environment "hle" I type in:
source /Users/admin/.conda/envs/hle/bin/activate hle
Once I've done this the pip command will reference the virtual environment location and not the standard mac location. So, when I install "mypackage" as follows:
pip install mypackage
It subsequently installs files in the virtual folder and not in the usual mac system folders.
You can find out about the Anaconda virtual environment (and download it) over here: http://conda.pydata.org/docs/install/quick.html but other virtual environments (like Virtualenv) work in the same way.
I am learning Flask and am trying to install it using virtualenv but the pip installs the flask folder in Lib-->site-packages instead of the main venv folder.
This question has been asked before, but the answer did not work for me because the OP used GIT Bash install. I didn't (using Windows cmd):
C:\Users\Owner\> cd Desktop
C:\Users\Owner\Desktop> md python_projects
C:\Users\Owner\Desktop> cd python_projects
C:\Users\Owner\Desktop\python_projects> md project1
C:\Users\Owner\Desktop\python_projects> cd project1
C:\Users\Owner\Desktop\python_projects\project1> virtualenv venv
C:\Users\Owner\Desktop\python_projects\project1\venv> cd Scripts
C:\Users\Owner\Desktop\python_projects\project1\venv\Scripts> activate
(venv) C:\Users\Owner\Desktop\python_projects\project1\venv\Scripts> pip install Flask
This is my first time using virturalenv and installing Flask, so the mistake may be silly.
I tried starting over and creating a brand new directory, and same results. I also tried using virtualenv venv --no-site-packages.
Am I not supposed to put the file in the main venv folder instead of it's Lib -- site-packages?
pip installs the flask folder in Lib-->site-packages instead of the main venv folder.
Not clear enough. I'm betting there is isn't a file name anywhere on your computer that reports its path as:
Lib-->site-packages
I don't use windows, but the whole purpose of virtualenv is to install python and all the packages you install into your virtualenv directory. However, that does not mean the contents of your virtualenv directory won't have lots of subdirectories. pip will locate all the packages you install somewhere in the subdirectories in your virtualenv directory.
And if you've ever installed packages without using virtualenv, at some point you will discover that they get installed into a directory named:
.../lib/python3.x/site-packages
which is located deep within the directory structure of your python install. virtualenv mimics that directory structure.
So, are you saying that pip installed the packages in:
/Lib/site-packages
or in:
.../your_virtual_env/Lib/site-packages
or, somewhere else?
I have my Django project folder and inside that i have my virtualenv folder
I have few questions
I have packages already installed in main installation and as well in virtual env. Dont those packages mix with each other. I mean if i have old version in main installation and new version in virtual env how does the system knows which one to choose
Suppose i move my project folder to new computer than can i use same virtual env folder because it was in the same app directory or i have to start all over again
How will i know that pip install package to virtual env or main installation
Unless you created the virtualenv with --system-site-packages, packages don't mix at all. If they did, Virtualenv has priority.
If the path doesn't change, there are chances that you can reuse it. You could make a virtualenv --relocatable if the path changes. But you should make a requirements file and be able to regenerate a fresh virtualenv in one pip -r req.txt command.
If a virtualenv is activated, pip will install in the virtualenv, it has priority.