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?
Related
I have created a Python application and now I need to deploy it to UAT and production environments which don't have access to pip library (the environment doesn't have access to internet).
I was told to put all the libraries in a folder python-lib. Should I just copy the all the content in anaconda3\lib\site-packages\ to the folder? However, it has several hundred mega bytes. Is it a way to copy the libraries just needed by my application?
What I would do is create a virtual environment locally and install the packages needed to it like so:
$ python3 -m venv prod_env
$ source prod_env/bin/activate
$ pip install <package_name>
If the folder is too large you can always compress and unzip on the UAT/Prod box.
Once the packages are installed in the virtual environment that are dependencies for your app, move the prod_env folder to the prod/UAT environment using scp or whatever method you normally would.
After the folder is moved to the environment you'll need to go into the bin folder and edit the activate script. You'll need to edit the VIRTUAL_ENV variable to be the path that the virtual environment is located on your prod/UAT box.
Once that's done you should be able to activate the virtual environment as normal:
$ source /path/to/prod_env/bin/activate
Using pip -t
mkdir libs
python -m pip -r requirements.txt -t libs
Then add the path to PYTHONPATH. Now sure if there is any default PYTHONPATH for a module/script.
For a project, I can't let users use pip install before running the app.
My project is a python flask app that I used pip to grab the dependencies. How do I bundle it so the apps can run without using pip install?
I've done this before. I created a virtualenv for my project so all dependencies (including the python executable) are contained within the project sub-directory tree. Then just zip up that directory tree. To install elsewhere, just unzip and run it.
I'm pretty sure python checks it's install directory for file dependencies and inside the same directory as the python file you are running. You can copy the packages from the pip install directory and place them in the same directory as your python file. It should see them.
It's pretty easy to make a bash script.
Open a text editor, save it with a .sh extension.
type in your commands.
This is a linux bash script:
#!/bin/bash
#This is a comment
sudo pip install -U memory_profiler
sudo pip install pympler
This is a windows batch script.
Save as a .bat file
ECHO
cd [pip install directory]
pip command
pip command
when python runs a file, it adds the current working directory to where it will look for modules to import.
you just need to install the requirements directly into your project folder.
the easiest way to do this is:
create a virtualenv somewhere and do your pip install -r requirements.txt
copy the contents of the site-packages folder from the environment ( $VIRTUAL_ENV/lib/python2.7/site-packages note: you might have to change accordingly for python version) to your project folder.
distribute your project folder.
edit: to add that I remembered that you're doing something similar to packaging an AWS Lambda.
This is one of my first times really using virtualenv and when I first activated it I was (and am) a bit confused about where my actual project (like the code) should go. Currently (after making and activating the virtualenv) this is what my project looks like in PyCharm:
Project Name
|-project-name <= I called my virtualenv project-name
|-bin
|-Lots of stuff here
|-include
|-Lots of stuff here
|-lib
|-Lots of stuff here
|-.Python
|-pip-selfcheck.json
In this environment, where should I put my actual code?
I don't recommend to put your project to virtualenv folder. I think you should do it in this way:
Do it in terminal if you're using Linux:
mkdir project-name.
cd project-name.
virtualenvwrapper env.
source env/bin/activate.
So you will have project-name folder where you will have all files according to your project + virtualenv folder called env.
If you don't have virtualenvwrapper, then just install it using apt-get:
sudo apt-get install virtualenvwrapper
When you activate a virtual env using virtualenv env, env (where all of your dependencies will be installed), sits at the top of your root directory. Let's say you use Django to create a project, you would then follow these steps:
Type source env/bin/activate to activate virtual environment
Type pip install django to install Django
Type django-admin startproject my-example-proj, which will install Django in your root directory
You should now how two directories: env and my-example-proj. You project never goes inside the env directory. That's where you install dependencies using pip.
I'm trying to run a certain script in Python, but it requires some other modules (setuptools) - I don't have write permissions for our /usr/ directory to install them, so I'm trying to install a local version of Python 2.7 to run it (not in /usr/).
When I try to run the ez_setup script for setuptools, it tries to access:
/usr/local/lib/python2.7/site-packages/
But that's not the installation I want (I get write permission error). I can point the ez_setup script to wherever, but I'm not sure how to get Python to use it. In my 2.7 install I ran:
site.getsitepackages()
['/usr/local/lib/python2.7/site-packages', '/usr/local/lib/site-python'].
Is there a way to change the default site-packages directory so I can do local installs? Like in the Python installation directory itself?
Thanks,
Kaleb
You really want to read up on Python virtual environments, which allow you to create a "local" Python tree into which you can install your own packages and such without requiring root access.
For example, assuming that you have the virtualenv command available (you may need to install this first; it's available as a package for most major distributions), you can create a new virtual environment like this:
virtualenv --system-site-packages myenv
The --system-site-packages option will make any packages in your system site-packages directory visible to this environment, as well as anything you install locally. Then activate the environment:
. myenv/bin/activate
And install stuff:
pip install apackage
NB For no good reason, pydoc will stop working for you in this configuration. You can just drop a new pydoc script into myenv/bin/pydoc that looks like this:
#!/usr/bin/env python
import pydoc
if __name__ == '__main__':
pydoc.cli()
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.