I try running code in Visual Studio code but I keep getting a ModuleNotFoundError. When I run the code in my terminal or in debug mode in VS with the activated conda environment it works fine.
System: Mac M1 12.3
Conda Environment, selected in Visual Studio Code.
I have added this
import os
print('cwd is %s' %(os.getcwd()))
import sys
print('executable is %s' %(sys.executable))
print('path is %s' %(sys.path))
and running in terminal gives:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
running in VS via Run Python File (upper right corner button) gives:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/lib/python39.zip', '/Users/USERNAME/miniforge3/lib/python3.9', '/Users/USERNAME/miniforge3/lib/python3.9/lib-dynload', '/Users/USERNAME/miniforge3/lib/python3.9/site-packages']
running in VS via Debug Python File (upper right corner button) gives:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
I am confused - How can I get this running in VS Code?
---- Update March 23 2022:
I have three options
When I add
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName"
}
to
settings.json
(see [https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm] 2 from #Kyouya Sato)
and run Run Code it works and I also get
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
without changing
settings.json
it does also not work using Run Code.
Run Python File is not working at all.
Workaround:
Set "python.terminal.activateEnvironment" to false in the settings.json.
Downgrade to the previous version of the extension which works fine(avoid conda run).
Try out the following VSIX which has the potential fix: https://github.com/microsoft/vscode-python/suites/5578467772/artifacts/180581906, Use Extension: Install from VSIX command to install the VSIX.
Reason:
Conda has some problems:
conda run -n MY-ENV python FILE.py uses the base interpreter
instead of environment interpreter.
Running using conda run inside already activated environment should
be the same as running it outside
conda run does not remove base environment components from
$PATH
Related
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.
I created my own python packages that contains some script. It can be installed with
python setup.py install
Under Linux I can then run the script when the correct conda environment is loaded with:
my_script.py
Under Windows the script is not found although I specify it twice in the setup.py file:
config = {
...
'version': '0.0.25',
'scripts': ['scripts/my_script.py', 'scripts/my_script.bat'],
'packages': ['my_package'],
'name': 'ms_package',
'data_files': [('scripts', ['scripts/my_script.py', 'scripts/my_script.bat']),
('static', ['static/my_data.csv'])]
}
The script starts with a shebang:
#!/usr/env python
The file is found, but when I run it I get the message that this file has not app associated. How to set this up correctly under Windows? Do I need a second file that specifies an app?
I added the my_script.bat that loads the anaconda environment, but then fails with the same error. And when I put
python my_script.py
in there, it does not find my_script. So, I would have to do something equivalent to
python `which my_script.py`
Is there a functionality like this on Windows (Windows 10)?
I would like to have an icon for the user to click on. For people that don't know how to use the command line.
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
So, I have Anaconda, OSGeo and Python2.7 installed on my computer.
I'm also using Spyder. In Spyder :
>>> import sys
>>> sys.executable
'C:\\ProgramData\\Anaconda3\\pythonw.exe'
Which is what I want.
However, in the windows command line and powershell :
$ python3
>>> import sys
>>> sys.executable
'C:\\Progra~1\\OSGeo4W\\bin\\python3.exe'
Which is not what I want. I want to use 'C:\\ProgramData\\Anaconda3\\pythonw.exe' (or python.exe, not sure) when using python3 in the command line.
Also :
$ pip3
Fatal error in launcher: Unable to create process using '"'
I don't get why python3 in the windows command line points to OSGeo's version of Python3. Here is my path :
C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Anaconda3;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files\PuTTY\;C:\Progra~1\OSGeo4W\bin\;C:\Program Files\Microsoft\R Open\R-3.4.0\bin
I also have an environment variable called PYTHONHOME
C:\ProgramData\Anaconda3
Moreover (for completeness of information), I have python 2 installed :
$ python
File "C:\ProgramData\Anaconda3\lib\site.py", line 177
file=sys.stderr)
^
SyntaxError: invalid syntax
($ pip outputs the same thing).
Having python3 and python2.7 both work when using python3 and python (respectively) in the windows command line would be a nice bonus, but it's not really my priority.
There are probably several things you have to take care of:
In general the search order of the Windows PATH is from left to right starting with the system PATH. The first matching element wins. In your case this is correct because the system will search C:\ProgramData\Anaconda3\ first. However in that folder is no executable called python3 by default. On my system I created a simlink pointing to python.exe. On your system you can do it in PowerShell like this:
New-Item -Path C:\ProgramData\Anaconda3\python3.exe -ItemType SymbolicLink -Value C:\ProgramData\Anaconda3\python.exe
pip is located in Scripts\ folder so in your case you have to add C:\ProgramData\Anaconda3\Scripts to your PATH and create the corresponding simlinks again. In this case you have to create two of them because pip.exe is appending its name to the script that is trying to call (i.e. if your exe file is called foo.exe it will try to call foo-script.exe which does not exist) you can create the simlinks in PowerShell with those two commands:
New-Item -Path C:\ProgramData\Anaconda3\Scripts\pip3.exe -ItemType SymbolicLink -Value C:\ProgramData\Anaconda3\Scripts\pip.exe
and
New-Item -Path C:\ProgramData\Anaconda3\Scripts\pip3-script.py -ItemType SymbolicLink -Value C:\ProgramData\Anaconda3\Scripts\pip-script.py
Like this you will be able to use python3 and pip3 from your cmd line. Please check for similar problems with your python2 installation folder.
Hope it helps.
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"
}