Remove sudo to run python script - python

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.

Related

Error while running Package in Command prompt , No module found

I have created a package named as patterns along with test.py,
test.py has the following code lines:
from patterns.shapes import *
square()
[Module Error]
I have copied the test.py file to the same location F:, where I trying to run via command prompt.
To gain access to this package from any location I have installed it using pip install . in the windows command prompt, which was successful, but when I try to access the file test.py I am getting the error as below :
F:\>py test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
from patterns.shapes import *
ModuleNotFoundError: No module named 'patterns'
My package folder is stored in a different drive.
While working on a python project, one must always work in a virtual environment. Please check if you have your virtual environment activated. If not, then follow the steps:
cd your_working_directory
virtualenv environment_name where environment_name can be any name you want.
environment_name/Scripts/activate if on Windows
Or environment_name/bin/activate if on MacOS
Then after activating the environment you can install all the dependencies using pip. i.e pip install patterns

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

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

ModuleNotFoundError When executing Python command inside bash script

The script runs fine from pycharm but can't see my my lib
-> site-packages when ran as a shell script. I've set my PYTHONPATH variable and added it to path as %PYTHONPATH% but no luck. I've checked bashrc and can't see the environment variable being overwritten. Maybe my structure is incorrect?
-lib
- site-packages
-scripts
-src
- monroes_events.py
**Error:**
(venv) C:\Users\James\repos\web_scraper\src>bash web_scraper_setup.sh
python3 Monroes_Scraper.py
Traceback (most recent call last):
File "Monroes_Scraper.py", line 5, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas
Install pandas module using pip install pandas which is required to run the Monroes_Scraper.py
You can find more detail here

Why doesn't module import while it actually exists?

My project structure looks like the following:
my-project:
dialog
utils
path.py
After install by 'pip install .', in PyCharm, I can import successfully either of the two:
from dialog.utils import path
import dialog.utils
Then I can refer to it as below:
path.module_path() // OR
dialog.utils.module_path()
It doesn't run into any problem. I can also do this in iPython's terminal. However, when I run a python through terminal:
python3 run_dialog
run_dialog.py import 'path' as shown above, it always reports this problem:
Traceback (most recent call last):
File "run_dialog.py", line 15, in <module>
import dialog.utils
ModuleNotFoundError: No module named 'dialog.utils'
So basically, I can run this code in both PyCharm and iPython terminal, but not in the project with 'python3 run_dialog'.
Why is that?
Try this
import sys
sys.path.append('my/path/to/myModule/dir')
import myModule
The Python interpreter needs to know to path to your imported module.
However, a better approach would be setting PYTHONPATH to your project directory like this
set PYTHONPATH=my/path/to/project
In my humble opinion, the Pythonic solution would be to install your package in the activated virtual environment using an "editable" install.
“Editable” installs are fundamentally “setuptools develop mode” installs.
use the pip install -e <path> option to do an editable install
(venv) $ pip install -e path/to/myproject # wherever myproject/setup.py is located
you can use pip list to confirm your package is installed into the virtualenv as an editable package, ie the path to the package is listed
$ pip list
myproject 0.1 path/to/myproject
pip 19.1.1
setuptools 41.0.1
changes saved to your package take effect each time you restart the interpreter and reload/import the package, letting you "edit" or "develop" your package

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 ?

Categories