Python fails to find module when invoked as script ; finds it interactively - python

I'm trying to run a python script containing submodules from Github to finetune its model.
The final step of the process is to invoke
python realesrgan/train.py -opt options/finetune_realesrgan_x4plus_pairdata.yml --auto_resume
However, doing from root of repo (and from realesrgan folder for that matter) so results in:
Traceback (most recent call last):
File "D:\Real-ESRGAN\realesrgan\train.py", line 5, in
import realesrgan.archs
ModuleNotFoundError: No module named 'realesrgan'
Yet, by launching python interactively from repo root (just "python") and then invoking
import realesrgan.archs
results in me being able to import it without any problem. All mentioned folders have required init.py in them.
This is my first time seeing discrepancy between calling a script and interactive python and I'm wondering, what can cause this.
System specifics just in case:
Windows 11
Python 3.9.9
python script execution fails from either powershell/cmd/file explorer

Related

Python3 ModuleNotFoundError when running from command line but works if I enter the shell

I think I'm missing something obvious here. I cloned this repo, and now have this directory structure on my computer:
When I try to run python baby_cry_detection/pc_main/train_set.py, I get a ModuleNotFoundError.
Traceback (most recent call last):
File "baby_cry_detection/pc_main/train_set.py", line 10, in <module>
from baby_cry_detection.pc_methods import Reader
ModuleNotFoundError: No module named 'baby_cry_detection'
However, if I type python and enter the interactive shell and then type the command
from baby_cry_detection.pc_methods import Reader
it imports just fine with no error. I'm completely baffled. I'm using a virtualenv and both instances are using the same python installation, and I haven't changed directories at all.
I think sys.path could be the reason that the module is not found when python command is executed. Here is how we can check if that is indeed the case:
In the train_set.py file, add import sys; print(sys.path). Looking at the error, the path may contain /path/to/baby_cry_detection/baby_cry_detection/pc_main. If that is the case, then we have found the issue which is that baby_cry_detection.pc_methods will not be found in the directory that sys.path is looking into. We'll need to append the parent baby_cry_detection directory to sys.path or use relative imports. See this answer.
The reason that python prompt successfully imports the module could be because the prompt is started in the correct parent directory. Try changing the directory to baby_cry_detection/pc_main/ and try importing the module.

made a shell to run script on startup, suddently it gives me an importerror

I followed this guide: guide to create a startupfile which excecutes a python file on startup.
in step 2 it says I have to test the startupfile I just created and suddently my script says:
Traceback (most recent call last):
File "Display.py", line 1, in <module>
import pyowm
ImportError: No module named pyowm
the python file works perfect if I run it directly.
what I allready tried: run pip again to see if the lib was okay
check the /usr/local/lib/python3.4/dist-packages folder to see if it was there and it is.
I think this is a python issue and not a RaspberryPi issue thats why I uploaded it here.
runned by:
sh launcher.sh
inside is:
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd /home/pi/arduino/Python/Main/Master
sudo python Display.py
cd /
Simple fix: define the version of python which will be used. It used python 2.7. Yet the lib was for 3.4.

GDB Python API: How to access events in Python

I am trying to write a Python script for GDB that uses the events gdb.events.inferior_call_pre and events.inferior_call_post. However, those events are not defined in gdb.events:
>(gdb) python print(dir(gdb.events))
['__doc__', '__name__', '__package__', 'cont', 'exited', 'new_objfile', 'stop']
The above was done without a program loaded. I have also loaded a C program, run it until a breakpoint, and then executed the command with the same result.
I am running CentOS 7 with gdb 7.6.1.
I downloaded and compiled the source (with --with-python=yes) for gdb 7.12 with less success:
(gdb) python print(dir(gdb.events))
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'gdb' is not defined
Error while executing Python code.
Any help is greatly appreciated.
According to gdb's NEWS file, support for the events gdb.events.inferior_call_pre and gdb.events.inferior_call_post was added in gdb 7.9.
If you're compiling gdb from source, the gdb python module can be found in the directory gdb-7.12.1/gdb/python/lib/gdb, which gets copied to gdb-7.12.1/gdb/data-directory/python/gdb.
When you do make install, it gets copied to <prefix directory>/share/gdb/python/gdb.
If the latter directory can't be found at runtime - for instance, if you run gdb from the source directory without having done make install - gdb ought to complain at startup:
$ ./gdb
Python Exception <type 'exceptions.ImportError'> No module named gdb:
./gdb: warning:
Could not load the Python gdb module from `<prefix directory>/share/gdb/python'.
Limited Python support is available from the _gdb module.
Suggest passing --data-directory=/path/to/gdb/data-directory.
The data directory also contains files with lists of available system calls for various target architectures. It's worth making sure that gdb has access to it.
If you can't run make install but have to run gdb directly from the source directory, cd to gdb-7.12.1/gdb and run ./gdb --data-directory=./data-directory .

Bash alias not working for Python script

Trying to set a Bash alias in order to run a Python script for Pygame.
I have the android.py acript in, /usr/local/bin/pgs4a-0.9.6/andoid.py.
Bash alias: alias pyg='python /usr/local/bin/pgs4a-0.9.6/android.py'
When I run python android.py when I am in the folder in executes just fine, but when I do it from any other folder or using the alias, I get the following error.
pyg
Traceback (most recent call last):
File "/usr/local/bin/pgs4a-0.9.6/android.py", line 11, in <module>
import interface
ImportError: No module named interface
Can anybody explain to me why this is?
Try this:
alias pyg='PYTHONPATH=/usr/local/bin/pgs4a-0.9.6 python /usr/local/bin/pgs4a-0.9.6/android.py'
That is, set your $PYTHONPATH environment variable to contain the directory with the code. You may need to adjust the PYTHONPATH to some other location, depending on where your interface package actually resides.

ImportError in Scripts directory

Using windows 7 and python 2.7. I have a package named Regetron in c:\Python27\Lib\site-packages\regetron which contains __init__.py and engine.py. When I try to run this library from the command prompt by typing regetron I get the following error:
Traceback (most recent call last):
File "C:\Python27\Scripts\regetron.py", line 6, in <module>
from regetron.engine import Regetron
File "C:\Python27\Scripts\regetron.py", line 6, in <module>
from regetron.engine import Regetron
ImportError: No module named engine
I added c:\Python27\Lib\site-packages\regetron to %PYTHONPATH% and can successfully import this module from other scripts located in other folders as well as the interactive prompt, but for some reason it refuses to run from the command prompt. What is going on?
You actually have two problems here. Fixing either one of them would actually eliminate your immediate error, but you need to fix both of them.
When I try to run this library from the command prompt by typing regetron
You shouldn't have a script named regetron and also have a module or package named regetron. Fix it by renaming your script. But if you want to understand why it's causing a problem:
The current working directory is always part of sys.path. So, you're in the directory with regetron.py in, and you run it with regetron. That means that regetron.py is on the path. So when you import regetron, it finds your script, not the package. Or, when you from regetron import engine, it finds your script, and tries to import a variable/function/class/whatever named engine from it, rather than finding the package and trying to import a module underneath it.
I added c:\Python27\Lib\site-packages\regetron to %PYTHONPATH%
Never add a package's directory to sys.path.
Since site-packages is already on your sys.path, the code in regetron/engine.py is already available as regetron.engine. You don't want it to also be available as engine. This will cause all kinds of problems.
So, rename you script to something else, remove regetron from %PYTHONPATH%, and everything will be fine.
But you may want to (re-)read the section on Packages in the tutorial.

Categories