From Windows 10 command line , Python 3.9.13, in directory:
C:\Users\foo.bar\wks\df\data
when trying to run :
python unilm\\layoutlm\\examples\\seq_labeling\\preprocess.py --data_dir FUNSD\\training_data\\annotations --data_split train --output_dir data --model_name_or_path microsoft/layoutlm-base-uncased --max_len 510
I get this error:
python: can't open file 'C:\Users\foo.bar\wks\df\data\unilm\layoutlm\examples\seq_labeling\preprocess.py': [Errno 2] No such file or directory
Notwithstanding the path C:\Users\foo.bar\wks\df\data\unilm\layoutlm\examples\seq_labeling\preprocess.py' is correct, really exists.
What's wrong?
There are a few ways to do this:
Use the -m flag:
python -m my_script.py
Use the sys.argv list:
import sys sys.argv.append('my_script.py')
Use the os.environ dictionary:
import os os.environ['PYTHONPATH'] = 'my_script.py'
Use the site module:
import site site.addsitedir('my_script.py')
The problem was in a typo in one of the words of the long path to a script. Thanks for trying to help and sorry for confusion!
Related
I successfully configured gurobi on centos 7 and it works if I try gurobi_cl on a test.lp file where the test.lp file contains just one line:
Minimize
Now i try to import gurobi from a script that is launched from httpd an I get this error:
From gurobi import *
File"/usr/lib/python2.7/site-packages/gurobipy/init.py" line 1 in
From .gurobi import *
ImportError: libgurobi90.so: cannot open shared object file:
No such file or directory
I am using gurobi9.0.1 and python 3.7 and I don't know why gurobi is calling python 2.7 in the log below.
Could you please help me?
Thanks
Solved the problem. I had to add gurobi_pi.conf under old.so.conf containing the path to my gurobi folder
ldconfig
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'm a beginner of Python and I want to run a python3 script in PyCharm Python Console, but it raise an error below:
>>> /Users/mymac/Documents/Python/test.py
File "<input>", line1
/Users/mymac/Documents/Python/test.py
^
SyntaxError: invalid syntax
I don't know what's wrong the file path is, how can I solve the problem?
use execfile('/Users/mymac/Documents/Python/test.py'). You are trying to run a python file like an executable. On top of that you are doing it in python interpreter.
Use python's execfile routine to call a file from interpreter or use python test.py to run it from the terminal.
I recognized that the answer I accepted ahead wasn't a perfect method, cause function execfile has been deleted in Python3, the alternative method is using open like:
>>> with open ('/Users/mymac/Documents/Python/test.py', 'r') as f:
... exec(f.read())
...
Still thanks for guys who helped me!
This could be several things. Have you set your environmental correctly? You can test this by cmd: python it should return Python 3.6.4 if that is your current version. If not then head over to tutorialsPoint for how to correctly set up your path.
If that is correctly configured, then have you selected an interpreter in PyCharm. If not, File --> Settings --> Project: Network --> Project Interpreter. Select your python installation path.
Another thing to note is that I suspect you mean to use the terminal instead of the python console.
Then in PyCharms terminal section, python test.py.
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.
I am using python 3.5.2 on windows10 machine and having problems running a py script.
I am getting this error: 'psutil has no attribute process_iter' if either use psutil.process_iter() or psutil.process.get_list().
I have psutil 5.4.3 installed.
Downloaded and tried this file https://pypi.python.org/pypi/psutil.. no luck
Here is the code:
import psutil
def processcheck(seekitem):
for proc in psutil.process_iter():
#for proc in psutil.get_process_list():
if proc.name() == seekitem:
....
for process in machine_process:
processcheck(process)
Any idea how can you get around this?
It works in Linux though.
Thank you.
I had this problem when my script was named psutil.py
Just rename your file if you have the same issue.