ModuleNotFoundError when running BASH script on CMD - python

I have a BASH script called run2scripts.sh that runs two python scripts at the same time. Here is the BASH script:
#!/bin/bash
python text_reader.py & python text_writer.py &
When I run the bash script on the CMD terminal, the result is:
>bash run2scripts.sh
File "text_reader.py", line 2, in <module>
import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
However, when I run the BASH script via Cygwin with $ ./run2scripts.sh, it works perfectly fine. Matplotlib works. I included in my .bashrc file export PATH="PATH TO PYTHON INTERPRETER" so python always goes to the path with all the modules installed. Furthermore, the command where python in CMD returns 2 results. C:\Cygwin64\bin\python and the path of the python interpreter with the modules installed. And CMD uses the latter path. The path to the interpreter is also apart of my PATH environment variable. So for these reasons, CMD shouldn't be returning a ModuleNotFoundError. print(sys.path) includes the path where Matplotlib is installed. The which python command in CMD however returns /usr/bin/python. Why is this happening and how can I fix it? I suspect the problem is related to the which python command and /usr/bin/python.

Related

Anaconda prompt: ModuleNotFoundError when running Python script

I am trying to run the following script "test.py" on Anaconda Prompt:
from tensorflow.keras.applications.resnet50 import ResNet50
...with the following command:
(conda_env) C:\dev>test.py
This results in the following error:
ModulNotFoundError: No module named 'tensorflow'
When I run the same script on Anaconda Prompt with the following command I don't get any errors:
(conda_env) C:\dev>python test.py
I have installed tensorflow in the Anaconda environment 'conda_env'
(conda_env) C:\dev\>conda env list
# conda environments:
#
base C:\Users\xx\Anaconda3
conda_env * C:\Users\xx\Anaconda3\envs\conda_env
keras_1 C:\Users\xx\Anaconda3\envs\keras_1
tf-gpu C:\Users\xx\Anaconda3\envs\tf-gpu
Why is this like that?
You won’t get errors if you do
(conda_env) C:\dev> python test.py
because then you’re following the correct syntax for running python scripts in a terminal. By adding python before the .py file, you initiate the Python interpreter that executes your script. Without it, the terminal won’t know what Python interpreter to use to execute your script and may end up using an interpreter that doesn't have the modules you require. There are ways to skip writing python before executing if that’s what you want.
For example, see: Calling a python script from command line without typing "python" first

Running python2 script in python3 loaded unix environment

my current bash shell is loaded with python3 but occasionally I tend to run old python2 scripts and I get this error
python/2.7.10/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory"
so every-time I had to load python2 back and forth. Is there a way to run python2 script in python3 environment by just changing any environmental variables?
Are you open to virtual envs? Otherwise you can run python2 or python3 from the command line.
As an example, create the following file, hello_world.py:
#!/usr/bin/env python
print "Hello, World!"
Then you can run from the bash shell:
python2 hello_world.py
That should work no problem. However, if you run:
python3 hello_world.py
You will run into the error:
SyntaxError: Missing parentheses in call to 'print'. Did you mean
print("Hello, world!")?

Run .py script via sh import module error

This is a very basic question on how to code in python and run your script from a very beginner.
I'm writing a script using Xcode9.4.1 which is supposed to be for python3.6. I then have an sh script run.sh, in the same folder of the script (say "my_folder") which simply looks like
python my_script.py
The python script looks like
from tick.base import TimeFunction
import numpy as np
import matplotlib.pyplot as plt
v = np.arange(0., 10., 1.)
f_v = v + 1
u = TimeFunction((v, f_v))
plt.plot(v, u.value(v))
print('donne!\n')
But as I try to run my_script.sh from the terminal I get a "ImportError: No module named tick.base" error.
But the tick folder is actually present in "my_computer/anaconda3/lib/python3.6/site-packages" and up to last week I was using Spyder from anaconda navigator and everything was correctly working, so no "import error" occurred.
The question is quite trivial, in some sense it simply is "what's the typical procedure to code and run python script and how modules are supposed to be imported-downloaded when running on a given machine?"
I need it since my script is to be run on another machine through ssh and using my laptop to make some attempts. Up to last year I used to work in C and only need to move some folders with code and .h files.
Thank for help!
EDIT 1:
From the Spyder 3.2.7 setting, where the script was giving non problem, I printed the
import sys
print(sys.path)
The -manually- copied the content to the sys.path variable in my_script.py and rerun 'run.sh' and now getting a new (strange) error:
Traceback (most recent call last):
[...]
File "/Users/my_computer/anaconda3/lib/python3.6/site-packages/tick/array/build/array.py", line 106
def tick_double_array_to_file(_file: 'std::string', array: 'ArrayDouble const &') -> "void":
^
SyntaxError: invalid syntax
First, check the python which you are calling the script with is pointing to the anaconda python and it is of the same version you are expecting it to be. You can do "which python" command in Linux and Mac to which the path which points to python. It if is pointing to some different version or build of python than the one which you are expecting then add the needed path to the system environment PATH variable. In Linux and Mac this can be done by adding the following line in the .bashrc file at the /home/ folder:
export PATH=/your/python/path:$PATH
And then source the .bashrc file.
source .bashrc
If you are on a operating system like cent os ,breaking the default python path can break your yum so be careful before changing it.
I am running a script in PyCharm and under the Project Interpretor I have the path
C:\envs\conda\keras2\python.exe
When I try to run the script via ssh on the server I get a 'no module named' error. I get
/usr/bin/python as the ans to 'which python' on the server itself. Could you tell me which path I must add for the script to run properly?

"python myscript" ignores "#!/usr/bin/env pythonX" where pythonX doesn't exist

Why doesn't test.py throw error env: python3: No such file or directory when Python 3 is not installed?
My system (Mac OS X) has Python 2.7 installed, but not Python 3:
$ /usr/bin/env python -V
Python 2.7.12
$ /usr/bin/env python3 -V
env: python3: No such file or directory
File test.py:
#!/usr/bin/env python3
import sys
print sys.executable
Executing test.py:
$ python test.py
/usr/local/opt/python/bin/python2.7
I thought that since Python 3 does not exist on my system, having the shebang line #!/usr/bin/env python3 will throw an error and terminate the script. But env actually selected the Python 2.7 interpreter.
The shebang is interpreted by OS when it tries to execute the script. Whey you type python test.py, the OS executes python and python executes the script (and python is found based on the current PATH) as opposed to being processed by the OS.
If you make the script executable (chmod +x test.py) and then try to execute it directly (e.g. ./test.py), the OS will be responsible for running the script so it will look at the shebang to figure out what program is responsible to run the script. In this case, it is /usr/bin/env which will look for python3 and try to use that. Since python3 isn't there (or not findable on your PATH) you'll see the error.
The shebang is only processed when you do test.py, running the file directly instead of running python with test.py as an argument. When you do python test.py, Python completely ignores the shebang line.

SublimeText3 cannot find Python modules (numpy) installed with MacPorts

I installed Python 3.5 using MacPorts. I am trying to use SublimeText3 as an editor. (Anything better and more integrated tan ST3 for python development??)
From the MacOSX terminal, I can 'import numpy' just fine, but SublimeText3 cannot find the packages.
Is it because the python packages are installed as 'Frameworks'?, because the Path for finding those modules is not right?, other????
Here's what terminal shows:
$ type -a python3
python3 is /opt/local/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.5/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.5/bin/python3
python3 is /usr/local/bin/python3
Here's what ST3 shows:
File "/Users/xxx/Desktop/python_work/array_play.py", line 1, in <module>
import numpy
ImportError: No module named 'numpy'
[Finished in 0.0s with exit code 1]
[cmd: ['/Library/Frameworks/Python.framework/Versions/3.5/bin/python3', '-u', '/Users/xxx/Desktop/python_work/array_play.py']]
[dir: /Users/xxx/Desktop/python_work]
[path: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin::/Library/Frameworks/Python.framework/Versions/3.5/bin/python3/site-packages]
As you can see, I tried to add
/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages
to the Path variable, since this is the location of the numpy folder, but this doesn't work...
Should I just ignore this message in ST3 and use it only as an editor??? Seems rather silly to have an IDE and not be able to build and run programs from it...
First, you need to find out which python3 you are using in terminal. You can do this by running command which python3. Suppose the output is: /Users/username/.anaconda3/bin/python3.
Second, try to set PYTHONPATH environment variable in Sublime's settings. Go to Preferences -> Settings User, and add the following lines at the end of the settings file (but inside of the last } symbol).
"env":
{
"PYTHONPATH":"/Users/username/.anaconda3/bin"
}

Categories

Resources