I am very new to Python.
I am in command line and I typed the following:
python -m pip install -U pip
Python then starts working inside of command line.
When I type
import pyperclip
pyperclip.copy('testing')
pyperclip.paste()
'Hello World!'
is printed.
When I go to the python shell and type
import pyperclip
pyperclip.copy('testing')
pyperclip.paste()
I get an error message:
ModuleNotFoundError: No module named 'pyperclip'
How can I figure out where command line is looking when I type import pyperclip and where it is looking in the python shell to compare the two?
Edit #1:
import sys
sys.path
Seems to be important. I am guessing that when a module is imported, python checks one of those locations.
pyperclip-1.5.7 is now located in one of the paths specified., the "Lib" directory.
pyperclip-1.5.7 looks like this inside of the folder:
All of the other modules are located as ".py" files just outside of the pyperclip-1.5.7, maybe I need to move some stuff there. I wonder how I can tell python to check inside of this directory...
Edit #2:
I went to this location: https://leemendelowitz.github.io/blog/how-does-python-find-packages.html
On the page, the author writes:
So Python will find any packages that have been installed to those
locations.
Let's see if I can add something to the sys.path location that points to the specific package that is inside the pyperclip-1.5.7 directory and if that helps.
I have both python 3 and python 2 as well. The problem I faced was even after successful pyperclip installation it was not working in Python 3.
The solution I got was to move to the directory of installation of Python 2
C:\Python27\Lib\site-packages
and copied the folders
pyperclip
and
pyperclip-1.7.0-py2.7.egg-info
to the python 3 directory which was in my case
C:\Users\MYNAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
I figured out what was wrong.
I also noticed that there are several posts all over in various locations of people reading the book
Automate the Boring Stuff with Python.
that are also struggling with this part.
After running
import sys
print('\n'.join(sys.path))
I started looking for differences between the output in command line and the output in the python shell. Everything looked extremely similar except in the directory:
lib/site-packages
I copied and pasted what was in this location, as pointed to in command line, to the location pointing to it in the python shell. I closed/reopened the shell and tried running the import pyperclip module, etc again and everything worked like a charm.
Problem is now solved.
Related
Introduction
I am using Python 3.9. I have seen quite a few answers online but I am struggling to make it work for me.
I saw that you can set default PYTHONPATH (PYTHONPATH=. python app/models/TestModel.py) but it sounds very hacky and I don't see how that would work should other devs try to use the code...
As someone who comes from a world of composer and node, I was expecting files to be pulled from a route dir but there seem to be some magic in the background that I am missing.
Issue
I have the following dir structure:
/app/models/TestModel.py
/modules/commons/models/BaseModel.py
Within BaseModel.py I print hello world:
print("Hello World")
Within TestModel.py I am trying to import my BaseModel
import modules.commons.models.BaseModel
The output if I am to call TestModel.py via CLI is below:
import modules.commons.models.BaseModel
ModuleNotFoundError: No module named 'modules'
Try python -m app.models.TestModel from the top of your package. In this case, python adds the current directory to the sys.path.
When you run python app/models/TestModel.py, python assumes app/models is the top of your package and adds ./app/models to sys.path.
Alternatively, you can put all your main entry points at the top level.
I have some code with the following structure:
+main-folder
+Foo-folder/
+foo
+bar
-file1.py
-file2.py
+bar-folder
+bars
I'm trying to do a simple import like so:
#Some module in +foo
from foo-folder.file1 import some-module
so in Pycharm it works fine but when I run it from the command line. I get moduleNotFoundError.
I'm using python 3.7 so I have no init.py files in them. Any idea about this?
Thanks,
When some module import works from PyCharm but not from the command line, it's usually because PyCharm automatically adds your project files to the Pythonpath.
In any case, also check your run configuration in PyCharm. What does it say is your working directory? Which directory are you starting it from when running from the command line?
So I builded a python package localy:
cgi#cgires:~$ pip list | grep mads
madscgi 0.1.0
Its nice! Afterwards I can use it in Jupyter Notebook, in iPython Shell, in Python Shell and even in python scripts outside the modules code. So it works as expected 100% outside the modules code:
Thats nice, but next I want to import code from one builded module (inside the package) into another python file (inside the package). Lets name it import_test.py and try it out:
So it fails if it is getting executed in the directory, where the package is build from. And it looks like, that the python interpreter is taking the parent directory (with the same name like the module) and this is failing.
Is is possible to enforce the usage of the installed pip-package?
As #MisterMiyagi pointed out, the problem was, that there were an upper folder which had the same name as the module.
Here: mads_cons is the upper folder from import_test.py. Therefore, the upper folder is getting imported instead of the via pip installed module. Thats it.
The file you want to import should either be in the same folder or referred to with the absolute path of it.
If that doesn't suit you, you can call sys.path
import sys
sys.path
You can keep your file in any of the directories sys.path returns.
Smart would be, if you keep the file inside.
......../site-packages/
I'm still a newbie at bash programming, but trying to run a program with little script. Reducing the problem to the error message, I have
cd /full/path/to/program
python3 -m krop
that is the command working when the actual folder is the /full/path/to/program
but if I run the same from root it doesn't work.
cd /another/path
python3 -m /full/path/to/program/krop
/usr/bin/python3: Error while finding module specification for '/full/path/to/program/krop'
(ModuleNotFoundError: No module named 'krop-0')
I tried lot of variants, but always the same output with errors. I do not have a clue of why the library "python3" adds the "-0" at the end of the name of the file.
What should I put to run the program from root?
python -m command expects a module name, similarly to the syntax you would import in a python program. So if your modules lies in ./directory and directory is a valid python module, you can do python -m directory.krop
You can't however index python modules from file system root. You have either to make your bash script run it in the good directory so you make a local import; or you have to package and install your module system-wide to make a global import that would be invoked with python -m krop from anywhere.
More information on packaging and installing modules: https://packaging.python.org/tutorials/packaging-projects/
Problem solved!,
It was a matter of managing the python import paths, as #hiroprotagonist replied. The list that contains all of directories python will use to search for modules, is available in a variable named sys.path.
So, if somebody wants to run a program (a 'library module as a script', according to python help) through python's command, from a directory different from the 'pwd' one, should write in the command line:
export PYTHONPATH='/full/path/to/program/'
python3 -c "import sys; print(sys.path)"
python3 -m krop
The second line is actually to print on screen, but the first one is the only necessary (export PYTHONPATH).
Thank you for the keywords and help!
Ps. May be should be edited the question title to "problem with a python command to run a program from command line on linux" or something like that.
Reference: python --help :)
I am working on widows10 with python3.7
I have downloaded opencv and numpy and sklearn(scikit-learn) and they are in C:\Python37\Lib\site-packages (where pip put them) and when I run
import sys
print(sys.path)
I get the output:
['', 'C:\\Python35\\Lib', 'C:\\Python35\\DLLs', 'C:\\Python35\\Lib\\site-packages', 'C:\\Users\\E6440', 'C:\\Python37\\python37.zip', 'C:\\Python37\\DLLs', 'C:\\Python37\\lib', 'C:\\Python37', 'C:\\Python37\\lib\\site-packages']
on cmd it says C:\Python37\Lib\site-packages which is one of the paths it looks in for when importing but then as soon as I run some code it gives a no module named ___ error
this is similar to the question python cant find module in sys.path but the comments in that one did not help me
edit
solved by using PYTHONPATH that was accurate to the python version
The package you're trying to import the module from may be available in the Python 3.5 installation too, but the package for Python 3.5 may be missing the module you're trying to import. Try moving the Python 3.7 paths ahead of the Python 3.5 paths in your PYTHONPATH environment variable.