I've attempted to start writing Python in WSL, but the whole experience has been super messy. To install packages, I know I want to use pip3, but any pip3 commands result in
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ImportError: cannot import name 'main'
My work-around has been to use the following command to install, which has worked:
python3 -mpip install --user somePackageName
My real confusion now comes from the fact that the which command is not locating packages installed by the above command. However, I know they are there as they appear when I run 'python3 -mpip freeze'. Is this because they are installed locally?
(Also If anyone has experience with Python and pip in WSL and could weigh in on how to go about configuring my Python environment, in order to avoid these types of issues, that would be super helpful.)
Thanks!
Try: python -m site
(perhaps replacing python by python3, etc)
You should get a short output with the following block,
From Windows:
sys.path = [
'<your current working directory>',
'C:\\Python38a2\\python38.zip',
'C:\\Python38a2\\DLLs',
'C:\\Python38a2\\lib',
'C:\\Python38a2',
'C:\\Python38a2\\lib\\site-packages',
]
From WSL:
sys.path = [
'<your current working directory>',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/home/<username>/.local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/dist-packages',
'/usr/lib/python3/dist-packages',
]
That's where you'll want to look for installed packages.
The order here is the import precedence (earlier entries "trump" later ones)
For pip installed packages, you'll generally want to look in the site-packages directory.
For <package manager> installed packages you'll generally want to look in the dist-packages directory.
Lastly, which is generally used to find executables on your $PATH environment variable (and, at least for me, requires that they have the executable bit set to appear in the results).
Here, you're looking for something different, modules or packages that are importable by Python. In other words, modules or packages on your $PYTHONPATH.
These are two different types of "path"s.
Inside your interpreter, $PYTHONPATH maps to sys.path and $PATH maps to os.environ['PATH'].
Related
I am following the steps stated here: How to use Stable Diffusion in Apple Silicon (M1/M2).
At my local MacBook M1 machine, I saved the below script in stable-diffusion.py file:
# make sure you're logged in with `huggingface-cli login`
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe = pipe.to("mps")
# Recommended if your computer has < 64 GB of RAM
pipe.enable_attention_slicing()
prompt = "a photo of an astronaut riding a horse on mars"
# First-time "warmup" pass (see explanation above)
_ = pipe(prompt, num_inference_steps=1)
# Results match those from the CPU device after the warmup pass.
image = pipe(prompt).images[0]
Now when I am trying to execute: python stable-diffusion.py from Terminal, I am getting following error:
Traceback (most recent call last):
File "/Users/apple/Desktop/area_51/stable-diffusion.py", line 2, in <module>
from diffusers import StableDiffusionPipeline
ModuleNotFoundError: No module named 'diffusers'
In order to fix it even I tried: pip install diffusers, however I still got same error.
Am I missing anything over here?
If you have already installed diffusers but are still encountering the ModuleNotFoundError, it's possible that the module is installed in a different Python environment than the one you are running your script from. In that case, you may need to check your Python environment settings and ensure that the module is installed in the correct environment.
To check your Python environment settings, you can use the following steps:
First, determine which Python interpreter you are currently using by running the following command in your terminal:
which python3
This should output the path to the Python interpreter that is currently being used. (I assume you're using python3. If you are using python2 for some ungodly reason, you should switch to python3.)
Next, ensure that the diffusers package is installed in the environment associated with the Python interpreter you are using. You can do this by running the following command:
python3 -m site
This will output information about the Python installation, including the location of the site-packages directory where installed packages are stored.
Look for a line that says "sys.path" or "USER_SITE" to find the location of the site-packages directory. This is the directory where Python looks for installed packages.
Check if the diffusers package is installed in the site-packages directory. You can do this by looking for a directory called diffusers inside the site-packages directory.
For example, if the site-packages directory is located at /usr/local/lib/python3.9/site-packages, you can check for the diffusers package by running the following command:
ls /usr/local/lib/python3.9/site-packages | grep diffusers
If the diffusers package is installed, this command should output a directory called diffusers. If the package is not installed, the command will not output anything.
If the diffusers package is not installed in the correct environment, you can try installing it using the appropriate package manager for that environment. For example, if you are using a conda environment, you can try installing the package using conda (conda install -c conda-forge diffusers). If you are using a virtual environment created with venv, you can try activating the environment and installing the package using pip (pip3 install diffusers).
I have python program that imports schedule (import schedule) at the beginning. The code executes without a problem with python3 command, but starting it from other python file with call("sudo python3 ProgramWithSchedule.py", shell=True) returns error ModuleNotFoundError: No module named 'schedule'. And I can't figure out why...
I have library schedule installed with pip, pip3 AND apt-get (tried all three just to be sure :)
Thanks!
Because you are using a different interpreter/virtual environment for each project, which is generally considered the best practice.
You can apply the command below to create a file with all your installed modules, so you can use them whenever you want, by a single command to install all.
To keep/save all modules in a file:
pip freeze > requirements.txt
To install all of them with a single command in a new interpreter/virtual environment:
pip install requirements.txt
In case you tried installing a package and get an output:>>Requirement already satisfied.
You will find a path in your output where it says Requirement already satisfied, copy the path. Now go back to your working environment.
import sys
sys.path.append("/the/path/you/copied")
import schedule
You can try to force the usage of the same python interpreter with :
call(f"sudo {os.getenv('PYTHON3')} ProgramWithSchedule.py", shell=True)
and call your-script.py with :
PYTHON3=$(type python3) your-script.py ...
For some odd reason no matter which package I install when I go to import it doesn't know what package I'm talking about. I am very certain this is a Visual Studio Code error but if not I am also using Linux.
When I pip install the package pyttsx3 this is what I get in the Terminal:
Collecting pyttsx3
Downloading https://files.pythonhosted.org/packages/24/4e/580726c73272344d3e74b7aaffae55ff6b6450061fbecb8cc6e112531c02/pyttsx3-2.7.tar.gz
Building wheels for collected packages: pyttsx3
Running setup.py bdist_wheel for pyttsx3 ... done
Stored in directory: /home/secretlloyd/.cache/pip/wheels/a2/8a/fe/11112aca9c89142c3a404bc67ef3393a7ad530da26639a05d4
Successfully built pyttsx3
Installing collected packages: pyttsx3
Successfully installed pyttsx3-2.7
But when I run a example I get this error:
Traceback (most recent call last):
File "/home/secretlloyd/Visual Studio Code/Python/Finished/Text Colors/finished.py", line 1, in <module>
import pyttsx3
ModuleNotFoundError: No module named 'pyttsx3'
You can use an virtual environment to install your libs. If you do that, each project will have its own scoped libs without affect your global libs.
How to use the virtual environment?
Enter the root folder of your project and then run the following commands on the bash:
$ py -m venv .env
$ source .env/Scripts/activate
After that you'll notice your bash will have a prefix like that (.env). Then you should install your libs:
(.env) $ pip install pyttsx3
In order to deactivate the virtual environment just run the following command:
(.env) $ deactivate
Setup VS Code Intellisense for Virtual Environment
If you're using VSCode you can set the correct python interpreter after setting up a virtual environment. Just follow the steps:
Open VSCode in your project
Press F1
Type: > python: select interpreter
Click on Enter path or find an existing interpreter
Click on Find
The navigate to .env > Scripts > python
3 possible cases:
The same thing happened to me when I did not notice I was using two Pythons at the same time one 2.7 and another one 3.6. Make sure to know where is your package being installed to the Python modules folder you really want to store it or in another one you did not know existed.
Your PATH might not be configured correctly, check out either if you are using Windows or Linux if your PATH variables are configured correctly. You can reset your configuration if you wish. (link= How to reload .bashrc settings without logging out and back in again?)
For some packages/libraries of Python the way of importing the library is different from the name you import it on your .py file. For example: You can install OpenCV library by [pip install OpenCV] but when importing it in a file you have to write [import cv2].
I hope you find this information helpful for your problem.
I'm trying to install Pandas using pip, but I'm having a bit of trouble. I just ran sudo pip install pandas which successfully downloaded pandas. However, it did not get downloaded to the location that I wanted. Here's what I see when I use pip show pandas:
---
Name: pandas
Version: 0.14.0
Location: /Library/Python/2.7/site-packages/pandas-0.14.0-py2.7-macosx-10.9-intel.egg
Requires: python-dateutil, pytz, numpy
So it is installed. But I was confused when I created a new Python Project and searched under System Libs/lib/python for pandas, because it didn't show up. Some of the other packages that I've downloaded in the past did show up, however, so I tried to take a look at where those were. Running pip show numpy (which I can import with no problem) yielded:
---
Name: numpy
Version: 1.6.2
Location: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Requires:
Which is in a completely different directory. For the sake of confirming my error, I ran pip install pyquery to see where it would be downloaded to, and got:
Name: pyquery
Version: 1.2.8
Location: /Library/Python/2.7/site-packages
Requires: lxml, cssselect
So the same place as pandas...
How do I change the default download location for pip so that these packages are downloaded to the same location that numpy is in?
Note: There were a few similar questions that I saw when searching for a solution, but I didn't see anything that mentioned permanently changing the default location.
According to pip documentation at
http://pip.readthedocs.org/en/stable/user_guide/#configuration
You will need to specify the default install location within a pip.ini file, which, also according to the website above is usually located as follows
On Unix and Mac OS X the configuration file is: $HOME/.pip/pip.conf
On Windows, the configuration file is: %HOME%\pip\pip.ini
The %HOME% is located in C:\Users\Bob on windows assuming your name is Bob
On linux the $HOME directory can be located by using cd ~
You may have to create the pip.ini file when you find your pip directory. Within your pip.ini or pip.config you will then need to put (assuming your on windows) something like
[global]
target=C:\Users\Bob\Desktop
Except that you would replace C:\Users\Bob\Desktop with whatever path you desire. If you are on Linux you would replace it with something like /usr/local/your/path
After saving the command would then be
pip install pandas
However, the program you install might assume it will be installed in a certain directory and might not work as a result of being installed elsewhere.
You can set the following environment variable:
PIP_TARGET=/path/to/pip/dir
https://pip.pypa.io/en/stable/user_guide/#environment-variables
Open Terminal and type:
pip config set global.target /Users/Bob/Library/Python/3.8/lib/python/site-packages
except instead of
/Users/Bob/Library/Python/3.8/lib/python/site-packages
you would use whatever directory you want.
Follow these steps
pip config set global.target D:\site-packages to change install path
or py -m pip config --user --editor notepad edit
[global]
target = D:\site-packages
set environment variable to use download import xxx
PIP_TARGET=site-packages
PYTHONPATH=site-packages
3.pip config unset global.target, to upgrade pip py -m pip install --upgrade pip
#Austin's answer is outdated, here for more up-to-date solution:
According to pip documentation at
https://pip.pypa.io/en/stable/topics/configuration/
You will need to specify the default install location within a configuration file, which, also according to the website above is usually located as follows
Mac OS
$HOME/Library/Application Support/pip/pip.conf if directory $HOME/Library/Application Support/pip exists else $HOME/.config/pip/pip.conf.
The legacy “per-user” configuration file is also loaded, if it exists: $HOME/.pip/pip.conf.
The $HOME folder can be located by navigating to ~/ (cmd+shift+G in Finder; cmd+shift+. to show hidden files).
Windows
%APPDATA%\pip\pip.ini
The legacy “per-user” configuration file is also loaded, if it exists: %HOME%\pip\pip.ini
The %HOME% is located in C:\Users\Bob on windows assuming your username is Bob
Unix
$HOME/.config/pip/pip.conf, which respects the XDG_CONFIG_HOME environment variable.
The legacy “per-user” configuration file is also loaded, if it exists: $HOME/.pip/pip.conf.
On linux the $HOME directory can be located by using cd ~
You may have to create the configuration file when you find your pip directory. Put something like
[global]
target = /Library/Frameworks/Python.framework/Versions/Current/lib/python3.10/site-packages/
if you are on a Mac. Except that you would replace /Library/Frameworks/Python.framework/Versions/Current/lib/python3.10/site-packages/ with whatever path you desire. If you are on Linux you would replace it with something like /usr/local/your/path
After saving the command would then be
pip install pandas
However, the program you install might assume it will be installed in a certain directory and might not work as a result of being installed elsewhere.
Please note that
pip3 install pandas
might be the solution if your packages gets installed in the Python2 folder vs Python3.
I try to install PyTables package using easy_install.
My problem is that I am not root on the system and am not allowed to write to /usr/local/lib/python2.7/dist-packages/ directory.
To solve this problem I decided to install locally. For that I have created a new directory: /home/myname/mylibs. Then I executed easy_install -d /home/myname/mylibs tables. As a result easy_install complained about the PYTHONPATH environment variable. To resolve this problem I added my new directory to the PYTHONPATH and repeated the previous command. As a result I get syte.py and syte.pyc file in my /home/myname/mylibs directory.
However, when I try import tables from Python it still does not work. What also worries me is that the new files do not have "table" in their names as well in their content.
Can anybody, please, explain me what is going on?
I had a similar problem recently trying to use easy_install to put packages in a location I had write-access to. Like you, I had to add the location to my PYTHONPATH variable to get it to install. It was then necessary to set PYTHONPATH every time I wanted to run a command using the installed package(s).
For example, to install and use the flask package I needed to do this:
$ PYTHONPATH=/tmp easy_install -d /tmp flask
$ PYTHONPATH=/tmp python -c 'import flask; print(flask.__version__)'
0.10.1
Without the PYTHONPATH variable set you'd get an error:
$ python -c 'import flask; print(flask.__version__)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named flask
You can of course set the variable permanently by running:
$ export PYTHONPATH=/tmp
Or putting export PYTHONPATH=/tmp in your ~/.bashrc file.