Bash alias not working for Python script - python

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.

Related

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

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

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.

Python Script not Running - Has to be something simple

OS: Fedora 21
Python: 2.7.6
I run a python script as root or using sudo it runs fine. If I run it as just the user I get the following:
Traceback (most recent call last):
File "/home/user/dev_ad_list.py", line 12, in
import ldap
ImportError: No module named ldap
selinux=disabled -- What other security is preventing a user from running a python script that imports ldap
If it works fine under sudo, it simply sounds like a file access issue.
A quick fix for this would be to run something along the lines of:
sudo chmod -R a+rX /usr/lib/python2.7
But you may wish to be more specific with the directory (or even file) that you actually apply this to.
Path to python was different than other user. User was pointing to canopy.

Why is PYTHONPATH being ignored?

I am setting PYTHONPATH to have a directory that includes a few .py files.
When I go into python and type "import file", the file cannot be find (it says "No module named wsj10").
If, however, I cd to the directory, and repeat the same process, then the file is found.
I am just not sure why PYTHONPATH is being ignored. I followed exact instructions from installation instructions of some software, so I know I am doing the right thing.
Any circumstances under which PYTHONPATH will be ignored, or import won't work?
Thanks.
Following a comment below, here is a transcript:
untar file1.tgz to file1/. file1.tgz contains a library/file called file1.py.
type in the shell:
export PYTHONPATH=`pwd`/file1/:./
echo $PYTHONPATH shows the variable was set.
run python and type "import file1"
I get the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named file1
If I do first "cd file1" and then to import file1 it identifies the file.
Any circumstances under which PYTHONPATH will be ignored, or import won't work?
Yes. I've set PYTHONPATH in my /home/me/.bashrc and all worked ok from terminal, but when Apache w/ mod_wsgi starts my python scripts, it acts under sysem or dedicated user, which knows nothing of my .bashrc.
For this particular situation, I just used apache config to set python path for apache (WSGIPythonPath option).

Categories