frustrations over python venv and global modules - python

I wrote a web scraping programming a couple of months ago, which I had python -m venv venv did a bunch of pip install, checked and doubled check with pip list, everything looks fine.
And today I wanna scrape again so I did my source venv/bin/activate checked with pip list, everything looks fine, and just keep having no module of this and that...
But when I deactivated it, it falls back to my global installed modules, my program runs perfectly fine. I could't believe it so i did :
rm -rf venv and
python -m venv source venv/bin/activate pip install -r requirements.txt
everything installed, check with pip list, and no module of this or that.
Anyone, please tell me what's happening?
thank you!

To ensure what python is being used and which python is linked to current pip:
which python to see where is the python executable (alternatively which python3, which python3.7, etc...)
running python show the version of python
pip -V shows the python to which it is linked
use python -m pip list to ensure the pip is linked to the python
python -m site shows were is the site-package located
writing python and hitting TAB multiple times shows what pythons are available in the path (os dependent)
This is more of a way to debug which python is being used.

Related

Installation in virtualenv is attempting to run the global Python binary and failing

Right now I'm trying to install python (3.10) and all further installations on my new pc (windows 10) and so far everything is set up:
Python installed
Windows paths for "Python" & "Python\Scrips"
I am able to call the python and pip version and also install some packages. But after installing virtualenv and creating one the - at the moment - unfixable error appears: I am unable to install packages into the pip-path of the virtualenviroment itself. Whenever I'm trying to run any pip-command I'm getting the following error:
Unable to create process using 'C:\Users\ExampleUser\AppData\Local\Programs\Python\Python310\python.exe "C:\folder\env\Scripts\pip.exe" '
As you can see, it's always refering to the original python-path, but on the other hand it's refering to the pip-path of the virtualenv!? Don't know if it's helpful, but when typing in where python and where pip the paths inside the venv are the first one listed. I've also watched out for no blank spaces in my path...
Unfortunately no explanation out there could help me until now and I never faced this problem on my old machine - mostly the same, except some older version of python, pip and virtualenv.
Does anyone else has an idea what I am missing?
downloading Python 3 at the official website and installing it via express installation
Copy & Paste the standalone python into the /python folder and overwriting the python version
running python -m pip install --upgrade pip in cmd
Now pip and python 3 are installed in their latest version.
It's work for me
Could you use venv to create your virtual environment, instead of virtualenv (given that venv is the recommended way to create virtual environments for Python 3.3, and newer)?
If using venv is an option, this procedure may give you some idea on how to do it.
I have not done any Python development on Windows, but I think the basics would be:
python3 -m venv your-env-directory
your-env-directory\Scripts\activate.bat
If using venv is not an option, maybe you can try specifying the -v flag when running your virtualenv command to increase verbosity so you can further troubleshoot what's going on.
try upgrade pip version python -m pip install --upgrade pip
[ Sorry if this answer turns out to be more of a comment than an answer. I only have 21 reputation, so I cannot comment ]
When trying to install pip packages and run python files, is the CWD (Current Working Directory) C:\folder\env\Scripts? If so, try chaning your CWD to C:\folder. I had a similar problem and doing this fixed it.
You may need to look into a cygwin environment, and look into a chroot or jail environment to run the application without conflict.
Have you tried to use virtualenv-wrapper-win module.
It helps me a lot to manage virtual envs
Life is much easier using Anaconda 3 (it's definitely bloated compared to normal Python though), or use the minimal Miniconda (barebone install, basically just Python + a package manager). You can download it here: https://docs.conda.io/en/latest/miniconda.html#windows-installers
Then you can make a new virtual environment super easy:
conda create -n myenv pip
conda activate
If you have multiple environments you do: conda activate [environment_name]
Now you're in your new environment with pip installed. And you get drop down menus in the Windows menu to get to your new environment too, so there isn't any searching required. They just appear. Now if you want to link Jupyter Notebook or Spyder to the installation, it takes more steps since you need more packages. I used this guide which basically activates Jupyter first, then Spyder IDE. https://medium.com/#apremgeorge/using-conda-python-environments-with-spyder-ide-and-jupyter-notebooks-in-windows-4e0a905aaac5
Since you created the environment with pip added you can pip install whatever packages you need. I had to do this recently with OpenBLAS backed NumPy and SciPy (the defaults from pip, not from conda). Now Miniconda is the closest thing to basic Python installation, and comes with some nice tools to make your life easier. Hopefully this is helpful.

Pip is not installed with python 3.7.3 even i did selected it on install

I installed python to my new computer (Windows 7). I needed to use pip to install packages but I couldn't find it in the folder where it was supposed to be located.
I have tried to bootstrap it
python -m ensurepip --default-pip
and get-pip.py didn't work either (Got a bunch of errors)
I have looked for the folder it should be in to make sure I was looking in the right place
import os
import sys
print (os.path.dirname(sys.executable) + '\Scripts\\')
which gives
'C:\Users\Dell\AppData\Local\Programs\Python\Python37-32\Scripts\'
as an output (Folder where I was looking in the first place). That folder is empty. I have 'show hidden files, folders and drives' on, I have searched for it from my entire computer, re-installed python and tried older (and 3.7.4 since it was released the time i noticed that pip was missing) versions, i have even added that empty folder to PATH and called for pip to make sure that it isn't just hidden from me. I have ran a repair and modify on installer too. Am I just missing some obvious step or is something wrong elsewhere?
Double-Click again on your python3.7 Windows x86 executable installer (i.e: setup.exe file). Then follow this pattern click on Modify > pip > add python to environment variable
Finally click on install and its done . * Note I am using python3.6.7 but the steps are same for python3.7.3
If you are having trouble installing pip just do the following things.
Install Python 3x normally.
Go to properties of my computer -> Advance system settings -> new -> give vairable name as Path and variable value as the location of Python scripts and save it in my case it is.
C:\Users\Atharva\AppData\Local\Programs\Python\Python37-32\Scripts
Restart your computer and open cmd.
now you can use pip commands.
You should consider upgrading via the 'python -m pip install –upgrade pip' command.
Thats all
Type the following command in a CMD window:
python -m ensurepip --default-pip
This will download and install pip as a package. This code worked for me.

How to install a Python package inside a virtual environment with Pip (OS X)

Edit:
I'm going to close this question as the reason its happening is different from my original assumption, and it's clearer to ask the question anew:
Pip installs packages in the wrong directory with virtualenv
The accepted answer doesn't directly answer the original question, but is a very useful overview.
Based on discussion below the issue is that even after
$ source ~/PycharmProjects/Practice/venv/bin/activate
$ pip install numpy
numpy is installed in /usr/local/lib/python2.7/site-packages
What could the reason for this be?
Original:
Using Python on OS X via Homebrew:
I've been trying much of the day to sort this out but either I get a must supply either home or prefix/exec-prefix -- not both error, or the package I try to install goes into totally the wrong place:
$ pip3 --version
pip 18.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
$ cd venv
$ pip3 install numpy
..... [snip with following error:]
"must supply either home or prefix/exec-prefix -- not both")
Using this hint
$ pip3 install numpy -t .
Then I get a new error,
`Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/.../pip-install-0fvveq3v/package/'
Searching around SO gives various possibilities involving pip install setuptools. but pip install throws the above error or installs in the wrong place. i.e. the solution involves something that's causing the error in the first place.
I tried to use the Python.org installer but it didn't install pip at all. (The custom installer showed the option checked but with size zero).
An introductory overview is available in this nice tutorial. Here is a good summary with more detail. But, if you renamed or moved the virtual env dir after its creation, it could break it. Create a new one from scratch: $ cd ~/PycharmProjects; python3 -mvenv newenv ; Activate: $ source newenv/bin/activate ; Install something: $ pip install colorama (same as pip3 install only if venv activated); Check: ls ~/PycharmProjects/newenv/lib/python3*/site-packages ; Deactivate: $ deactivate
Then you could try this solution for Pycharm: how to associate a virtual environment with a python project in pycharm. PyCharm indeed comes bundled with virtualenv which could have been customized, please look into Pycharm-specific resources: creating virtual environments and installing packages in Pycharm.
If you have installed PyPI's mainstream virtualenv, by default it will create new environments with the python interpreter that virtualenv was installed with. But it's possible to specify an alternate Python Interpreter upon a new env creation: $ virtualenv -p python3.7 newenvname
Regarding the error DistutilsOptionError: must supply either home or prefix - please check this and this for solutions. Homebrew'ed mappings between python and pip are described here. The normal pip install --user is disabled in homebrewed Python, but there are workarounds. MacOS system Python doesn't provide pip, but it can installed, reinstalled or upgraded for any specific python version manually. Original non-brewed installers are also available for all Python versions: https://www.python.org/downloads/mac-osx/
By default there's no pip.conf, but it can be created by hand to customize things. All possible pip.conf locations (per-user, per-venv, and global/system-wide, and how they override each other) are listed here. If someone faces an issue, they could use pip config list command to see their active configuration, or locate pip.conf and find it.
Finally, you may want to ensure you aren't using pip against macOS' system python. Shell commands such as $ brew info python, which pip, which pip3, pip3 -V, which python3 can help you see what you actually use. Since the macOS default $PATH used to be /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin, stock macOS binaries (including python) may take precedence over some homebrew'ed installations (including python). If so, a custom PATH could be exported via the ~/.bashrc if required.

Python does not find installed modules

Python3.5 does not locate installed modules when invoked in virtual env.
Create virtual env: python3.5 -m venv autogit/venv && cd autogit
source venv/bin/activate
which python == ...autogit/venv/bin/python
Weird, would expect python3.5
Add my python source code to /autogit and pip freeze>requirements.txt
pip install -r requirements.txt
ls venv/lib/python3.5/site-packages shows request-0-0-0-py3.5.egg-info and some other stuff
Since dependencies are installed under python3.5 and which python revealed python rather than python3.5, lets invoke the python3.5binary explicitly.... venv/bin/python3.5 autogit.py
Get ImportError: No module named 'request
??? Where could python be looking for packages if not in my virtual env?
UPDATE The questions above remain unanswered; here are things I noticed since then and the workaround I used:
pip install produced a file request-0-0-0-py3.5.egg-info. It did NOT produce an actual request directory with the source code or binaries for this module. Also why is it version 0 0 0 that is fishy
After some googling I noticed the module I wanted seemed to be named requests not request which is what was in my source. I changed it requests, pip install, and everything works. It was hard to see that there was a mistake because pip installing request did not fail
Have you got other Python versions installed? That might be the problem.
Try using pip3 instead of pip

Can't get Django to work on my Mac

I'm having difficulty getting Django to work on my Mac. I pip installed it, as well as downloading it on PyCharm. I have a feeling it will work on PyCharm if I knew what I'm doing. I dont. haha. It's my first time. I am trying to work off the tutorial that they provide on their site. Here is where I run into trouble.
In terminal I type:
python -m django --version
and I get:
/usr/bin/python: No module named django
but when I type:
pip install Django
I get:
Requirement already satisfied (use --upgrade to upgrade): Django in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
The tutorial wants me to type:
django-admin startproject mysite
and I get this:
-bash: django-admin: command not found
So to my question. What is going on here? I'm thinking my path to it is different than what is expected, though I'm not fully sure. If you know the startproject mysite could you give it to me? Meaning, if you know the folders and content I think I could get it running on PyCharm. My PyCharm says it's been downloaded, so I think this would be a great way to go into it.
Many thanks.
From Comments
Looks like a virtual environment problem. Without going into the extreme details of your environment settings, try the following: make and navigate to an empty directory, type pyvenv-3.5 ./Env, then source ./Env/bin/activate, next pip install django, and finally python -m django --version. This virtual environment should work and be less prone to other odd PATH problems.
Additional Info
You will need to run the command source ./Env/bin/activate when you open up a new shell or run a bash script in order to active this environment.
Also, you can now manage your pip packages (including django) by using pip freeze > ./requirements.txt to create a lists of your packages w/ version numbers and 'pip install -r ./requirements.txt` to install the packages.
Seems to me that pip is not configured with the python that you are using. From the output you posted, pip is installing Django for the python executable residing in here:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
what is the output of ls -l $(which python) ?
This will tell you where the python you are using is. If it's different than the path above, pip is installing packages on another python executable.
You have 2 quick options.
Put a softlink to the python residing in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages inside your /usr/bin/ or wherever is imported before the path that which python shows.
ln -s {target-filename} {symbolic-filename}
which is probably
ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /usr/bin/python
Use a Virtual Environment.
pyenv ./env
source ./env/bin/activate
Now you are working on a virtual environment, which has its own pip and python so you should be fine to do anything you'd want !
Hope it helps

Categories