ImportError when execute script from command line - python

When executing script from command line, I got the following error
ImportError: No module named SubPackage3.MyClass
My package is structured as
TopLevelPackage
..SubPackage1
..SubPackage2
..SubPackage3
..__init_.py
..README.md
python (/usr/bin/python)
From other posts it seems that the problem is that the system does not recognize the path to SubPackage3. I have a separate __init_.py in each of the SubPackage, and print(sys.path) gave me
/home/shaunz/workspace/TopLevelPackage/SubPackage1
/home/shaunz/workspace/TopLevelPackage
/usr/lib/python2.7
....
Notice that only Package 1 is in the list of paths recognized. Any idea why this may be the case? And how do I change it?
I'm trying to run a module in SubPackage2. I'm using Eclipse.
Thanks

Related

Imported module cannot be found when Python script is run via a bat file

I know there has been similar problems, but unfortunately most of them are related to errors with pyperclip itself instead of the batch file, which i suspect is where the problem stems from.
Below is an MRE of my Python script:
#! python3 -> Do I have to use my version(3.8)?
# pw.py - An insecure password locker program.
import sys, pyperclip
#do something with the module
And my batch file pw.bat:
#py.exe C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py %*
#pause
I am running python 3.8 on windows 10. I imported the pyperclip module in my python script pw.py and ran the file via pw.bat, and this in turn gives me this error:
Traceback (most recent call last):
File "C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py", line 7, in <module>
import sys, pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Press any key to continue . . .
Which shouldn't happen as I have installed pyperclip on the project using pip, and the script itself runs just fine in pycharm. What am I missing?
EDIT: I forgot to mention that I am using pycharm. So the thing is that pycharm had also installed python.exe in the project folder. And as the module pyperclip is only installed to that folder, the python.exe used in the bat must point to the one in the project folder.
i don't know why are you using py.exe. when running commands from a batch file or cmd .you should use python.exe.obviously you would need to add python to add for doing so.instead of adding py.exe to path,add python in system variable Path which is somewhere present in C:\Users\[username]\AppData\Local\Programs\Python\(your path might be diffrernt).you can add python in Path by following this post
after adding python to path just use the following batch-file:
#echo off
python path-to-your-py-file\filename.py

python gives ImportError: No module named "" when triggered in a perl program

In my perl program which runs the python script
I have provided the PYTHONPATH env param with the path for the lib and i have run the python script. I am getting
ImportError: No module named "....."
my $script = "/path/pythonscript.py";
$ENV{'PYTHONPATH'} = "/path/lib";
system("python $script");
Whereas when i run the same python script on command line in the same directory where the script executes in my perl program, it is working.
Can anyone give me some pointers on why this is happening.
Try printing the contents of sys.path and compare the difference e.g. change your python script to
import sys
print(sys.path)
Most likely there is a difference here and this is causing the module to not be found.
I once had a similar problem. I solved it by creating an executable script (chmod) and making that script run instead of the python script. The script simply contained a cd to the directory and a python3 program. py

Import statement works on PyCharm but not from terminal

PyCharm 2016.2.3, Mac OS X 10.11.1, Python 3.5 (Homebrew);
I have this folder structure
project
/somepackage
/subpackage
__init__.py
bar.py
__init__.py
foo.py
foo.py:
import somepackage.subpackage.bar
print("foo")
bar.py:
print("bar")
So my expected output is
bar
foo
This works fine when run from PyCharm. However, when I run it from my terminal I get an ImportError:
$ pwd
$ /home/project (not the actual path; just omitting some personal stuff)
$ python3.5 somepackage/foo.py
File "foo.py", line 1, in <module>
import somepackage.subpackage.bar
ImportError: No module named 'somepackage'
I have found this question, which is about the same problem. However, none of the suggested solutions work for me, as I am indeed using the same Python interpreter as PyCharm does and I am currently in the folder that contains the /somepackage folder.
Does anyone have any other suggestions about how to solve this issue?
You are running foo.py like a script, but you are really using it like a module. So the proper solution is to run it as a module:
python3 -m somepackage.foo
For the record, another alternative is to edit your path like:
export PYTHONPATH=.
(Or you could put the absolute directory in there, and of course you should append any other directories that are already in your PYTHONPATH.) This is closer to what PyCharm does, but is less philosophically correct.
Setting PYTHONPATH is what makes it work, as noted above. I use the following VSCODE .env content so that it works for any project:
PYTHONPATH=${PROJ_DIR}:${PYTHONPATH}
This is essentially what PyCharm does when you check "Add Content Roots to PYTHONPATH" in your run/debug configuration. It's a helpful setting, but it spoils you because your code fails outside PyCharm.
Or, if you run in terminal, first export:
export PYTHONPATH=...
Took me days to work all this out.
i solved my problem by two steps on Linux:
first step
go to the root directory of your project and set:
export PYTHONPATH=$PATHONPATH:`pwd`
second step
run python3 -m somepackage.foo
remember Without '.py' suffix
I just had the same problem using scapy.layers.http Module,
this problem occurred on my Kali (linux-Debian) but run fine on Win-10 (after few modifications.)
packet was installed (scapy-http) correctly and the program was running in PyCharm but not as script (from terminal)
I tried solving it with reinstalling in main root, and messing with the sys.path but None have worked.
Troubleshoot & Solution
I found that it looks for the http module in:
/usr/local/lib/python3.7/dist-packages/scapy/layers/init.py
and got the ImportError:
from scapy.layers import http --->
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'http' from 'scapy.layers' (/usr/local/lib/python3.7/dist-packages/scapy/layers/__init__.py)
So then I checked where scapy-http is really installed, which is module called http.py, so I just copied it to:
/usr/local/lib/python3.7/dist-packages/scapy/layers/
found the http.py file in :/usr/local/lib/python3.7/dist-packages/scapy_http/
And that did it :)!
I know its a bit glitchie but that worked!

ImportError: No module named 'xx' in mac and linux

I am right now using the mac and trying to use the module pypng. I have installed the module from pip install pypng. It works well. However when I run my application python test.py (where inside test.py has 'import png'), it gives me a error saying 'ImportError:No module named png'.
However when I go into the python environment from command line, I type 'import png', it works without a error, why is that. I even restart the computer, but still in trouble.
when I installed pypng module, it tells me it is install in /Users/abc1/anaconda/lib/python2.7/site-packages, where inside this directory I can find the png.py and png.pyc.
when i print sys.path, this is the output, looks like it didn't load the png package from /Users/abc1/anaconda/lib/python2.7/site-packages. how do i solve it
['/Applications/Blender/blender.app/Contents/Resources/2.76/scripts/addons_contrib', '/Applications/Blender/blender.app/Contents/Resources/2.76/scripts/addons', '/Applications/Blender/blender.app/Contents/Resources/2.76/scripts/modules', '/Applications/Blender/blender.app/Contents/Resources/2.76/scripts/startup', '/Applications/Blender/blender.app/Contents/MacOS/../Resources/2.76/scripts/modules', '/Applications/Blender/blender.app/Contents/Resources/2.76/python/lib/python34.zip', '/Applications/Blender/blender.app/Contents/Resources/2.76/python/lib/python3.4', '/Applications/Blender/blender.app/Contents/Resources/2.76/python/lib/python3.4/plat-darwin', '/Applications/Blender/blender.app/Contents/Resources/2.76/python/lib/python3.4/lib-dynload', '/Applications/Blender/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages', '/Applications/Blender/blender.app/Contents/MacOS/../Resources/2.76/scripts/freestyle/modules', '/Applications/Blender/blender.app/Contents/Resources/2.76/scripts/addons/modules', '/Users/xisizhe/Library/Application Support/Blender/2.76/scripts/addons/modules']
Maybe you installed png to your python environment in the command line but not the environment you run your script. Try to use python batch.py in command line, or append /Users/abc1/anaconda/lib/python2.7/site-packages to sys.path.
Do you have a directory in your project that is named png? If so python will try to import it instead of the actual module

google ad exchange request.py run error in eclipse

When debugging my "google dobule click exchange testing script", it runs successfully on a Python Terminal, but when doing so with the eclipse pydev IDE, the following line give me an error:
import realtime_bidding_pb2
The error I'm getting is
ImportError: No module named realtime_bidding_pb2
I know this module is outsourced, but I don't know where it is or how to import it into the eclipse pydev IDE.
If you use unix like OS, change directory to the directory where the files requester.py, Makefile, realtime-bidding.proto (from requester.tar.gz) and execute command 'make' in shell. realtime_bidding_pb2 file will be created.
Next you get "ImportError: No module named google.protobuf". You need install python-protobuf (command 'apt-get install python-protobuf' for Ubuntu, you should be root).
Uses requester.py by command like 'python requester.py --url= --max_qps=1 --seconds=20'.

Categories

Resources