Anaconda prompt: ModuleNotFoundError when running Python script - python

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

Related

ModuleNotFoundError when running BASH script on CMD

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.

Different directory using python3.8 instead of python3.7

I tried to open a file named (choony.py) from the terminal and I got an error.So,
this is what I typed in the terminal:
python3 choony.py
This is the error that I got
No such file or directory
I tried this in the terminal
echo $PATH
This is the result that I got
/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
As you can see, python is running 3.7
how can I make python 3.8 the defult.

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!")?

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"
}

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

Categories

Resources