Where do python modules get saved to in a root directory? - python

So, I'm a bit new to python and have mastered javascript.
I know that javascript modules are saved in node_modules folder in a directory
I did: $ pip install pyautogui
but when I'm about to run my code, it says :
'Traceback (most recent call last):
File "bruh.py", line 1, in <module>
import pyautogui, time
ImportError: No module named pyautogui'
where do python modules get saved to? 'python_modules' folder?????
also, idk what to do, can anyone help me with this error?

To see the path where your modules are saved once you pip them.
You can open a python shell, import sys and print the sys.path variable. One of the path would end with site-packages, thats the path where modules are saved.
$ python3
>>> import sys
>>> print(sys.path)
[‘’, ‘/Users/uname/.local/lib/python3.6/site-packages/‘]

could be a number of things. do you have multiple versions of python on your machine? If so you'd need to pip install to the correct python version, e.g.:
pip3 install pyautogui vs pip install pyautogui

Related

ModuleNotFound error but module has just been installed

I'm using Windows 10, Python 3.6.4. I'm trying to use the module Pyperclip and have installed it with pip:
c:\Users\Bertie>pip install pyperclip
Requirement already satisfired: pyperclip in c:\python36\lib\site-packages (1.8.0)
But when I try to run a program which uses this module, I get this error:
c:\Users\Bertie\scraping.py test
Traceback (most recent call last):
File "C:\Users\Bertie\scraping.py", line 3, in <module>
import webbrowser, sys, pyperclip
ModuleNotFoundError: No module named 'pyperclip'
How can I fix this? Thank you.
The reinstall trace shows you install the packge successflly for python36.
Check if there is more than one python in your system. Type "python" in your windows cmd console, and check the python version to see if python36 is the default one. Then explicitly use the python interpreter to start the script "python your_python_script.py".
Where is the py file? Maybe try cd-ing into the folder and run python (python3) scraping.py?
Also check that you have installed the correct interpreter and are using the correct version of python.
Maybe You Installed pyperclip in conda env or a virtualenv and you forgot to activate it ?
Because This Has Happened To Me Many Times.
Or Maybe You Have 2 Instances Of Python installed on your computer and you accidentally installed pyperclip in the other instance of python ?

I have uploaded a Python package in pip, then pip installed this package, but I can't import this package in Python Interpreter

I have just uploaded a Python package into pip called hqc:
https://pypi.org/project/hqc/0.0.1/
then successfully pip installed the package using:
pip install hqc
but I can't seem to import the package in Python Interpreter. When I do this, I get the following error message:
>>> import hqc
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import hqc
ModuleNotFoundError: No module named 'hqc'
What am I missing? Do I need to set the environment variable PATH or is it something else?
In the folder where setup.py is, make a folder called hqc and put an empty file called __init__.py (thats two underscores on each side). Right now, there's no file to import so python fails when importing it.
hqc-0.0.1:
hqc:
You can test the package by going to the directory where setup.py is and running
pip install .
Before trying again, be sure to uninstall
pip uninstall hqc
I had a quick look at your github repository and found that the package name, i.e. your folder name is skltemplate.
So, your import will be: (verified it to be working)
import skltemplate
And found that skltemplate package exposes the following modeules:
TemplateClassifier, TemplateTransformer and TemplateEstimator.
You can import the above by doing:
from skltemplate import TemplateClassifier
Now, if you want your package to be named as hqc, you need to rename skltemplate directory to hqc AND run the setup.py again. (You can refer this stackoverflow answer for more details).
Ensure that you increment the package version inside setup.py before running it.

New to anaconda and spyder. using different sys.executables

For some reason when I install a package through Anaconda, it is not available in Spyder. When I execute the following command in anaconda and in spyder I get different files.
Anaconda:
import sys; sys.executable
'C:\\Users\\onp1ldy\\AppData\\Local\\conda\\conda\\envs\\deeplearning\\python.exe'
Spyder:
import sys; sys.executable
'C:\\Users\\onp1ldy\\AppData\\Local\\conda\\conda\\envs\\deeplearning\\pythonw.exe'
Can anyone help me with this? I am not sure what to do...
By Default: You can control which one of the executables will run your script. Such as, when opened from Explorer by choosing the right file_name like:
1. python.exe is a terminal-based (console) application to run and lunch CLI-Type Python-scripts.
*.py files are by default associated (invoked) with python.exe
2. pythonw.exe is a GUI-based app for lunching Graphical User interface-(No_UI_at_all_Scripts)
*.pyw files are by default associated (invoked) with pythonw.exe
TO SUMMARIZE AND COMPLEMENT MY OPINION:
First of all, Python-Binary that you're trying to run doesn't have Package installed. It does have a directory path named as package_name like torch at the search path of modules, and it is being treated as a Package namespace like for me:
torch.Tensor(5, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'Tensor'
For your current python binary, you need to install your package correctly. Visit Reference: Home-Page
python3.7 -m pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp35-cp35m-manylinux1_x86_64.whl
python3.7 -m pip install torchvision
More importantly, replace the pip or pip3 as the Home-Page instructions use with python3.7 -m pip;. In the end, don't forget to include python3.7 to be the full path to your Python binary.
Run this in spyder and see if you can access the package.
import subprocess
subprocess.call('pip install numpy', shell=True)
import numpy
based on your description the problem may be connected to the Python interpreter that you are using in Spyder. There is a similar issue on Stack Overflow at this url:
Issue on Anaconda and Spyder
You can try the solution proposed by Bremsstrahlung.
Hope this can help you.

ImportError: No module named 'gammu'

I work on Ubuntu Xenial (16.04) with python3, I also installed anaconda.
I installed python3-gammu (with apt install python3-gammu or/and pip install python3-gammu) to test send SMS.
Just run python3 console and
>>> import gammu
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gammu'
import sys
print(sys.path)
only return anaconda paths !
If I run
sudo find -iname gammu
…
./usr/lib/python3/dist-packages/gam
…
so if I add this path:
>>> sys.path.append('/usr/lib/python3/dist-packages/')
>>> import gammu
and it works !
Could you clarify this library path issue?
just
export PYTHONPATH=$PYTHONPATH:/usr/lib/python3/dist-packages/
To keep it at next reboot, put this line in your ~/.bashrc :
# added by Anaconda3 4.2.0 installer
export PATH="/home/my_user_name/anaconda3/bin:$PATH"
export PYTHONPATH="/usr/lib/python3/dist-packages/:$PYTHONPATH"
to active new .bashrc, do not forget to run
source ~/.bashrc
When you are trying to import any package it will check sys.path, which contains all paths of packages. If it find the package you want to import it will import it.
sorry for bad english...
Why use sys.path.append(path) instead of sys.path.insert(1, path)?
You may get clarity after seeing this ?

Remove sudo to run python script

Every time that I need to run my python program with:
python my_program.py
I get some error saying that some import was not found.
Some error like this:
Traceback (most recent call last):
File "graphic.py", line 1, in <module>
import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib'
Than I run:
sudo python my_program.py
And every thing works just fine. How do I remove the sudo command to run my python codes?
ImportError: No module named 'matplotlib' happens when your Python doesn't find the module. sudo changes the enviornment variables; That's why.
To fix this, locate where matplotlib is installed in your computer, and verify the folder is part of your sys.path.
import sys
sys.path
['C:\\Python27\\tests', ..., ...]
Then you've two options: insert that path from your script, i.e adding a line such import sys; sys.path.append(<folder>) or configure PYTHONPATH env variable under your user appending the folder to the path.
PYTHONPATH env variable gets loaded to sys.path on startup.
Best solution to me is a general workflow for all projects: use virtualenviroment]1.
sudo pip3 install virtualenv
virtualenv myenv
source mynenv/bin/activate
Then you should install your libraries again with pip and they will be installed in your virtualenviroment, isolated from everything else.

Categories