ModuleNotFoundError when using Visual Studio Code - python

I am using a machine I inherited with an Anaconda 3 installation on it. I am trying to complete a Python course, which is my first foray into Python ever. When I open a Python interpreter in Command Prompt or PowerShell, and I run the following line, it executes without error.
(base) PS D:\ProgramData\Anaconda3\Scripts> py
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlsxwriter
Somehow Anaconda is integrated with VS Code, yet when I try and import xlsxwriter in this window, I get the error:
ModuleNotFoundError: No module named 'xlslwriter'
When this Python interpreter window opens, it shows the following:
Jupyter Server URI: http://localhost:8889/?token=b8a07e61e603f9dae0ee599198ac33f68d3f398036f6af0e
Python version:
3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
(6, 0, 1)
D:\\ProgramData\\Anaconda3\\envs\\py37\\python.exe
If I try installing xlsxwriter with pip, I get a message that
Requirement already satisfied: xlsxwriter in d:\programdata\anaconda3\lib\site-packages (1.2.8)
I'm not asking for an instant answer, but guidance on how I can diagnose and correct this problem. Why is the module available to some interpreters (Command Prompt and PowerShell, and Jupyter Notebook) and not available in the VS Code interpreter? How can I use VS Code and or Python functionality to learn about the config of the current environment and such things?

Based on advice given in this answer, I ran the command
!pip install xlsxwriter
in the VS Code interpreter, and finally, the module was available for import.

Related

Python 3.6.x + Windows: Interpreter just shuts down for any external library

Due to external dependencies I need to run python 3.6.x (I know it is EOL) on Windows.
However, any external library causes the interpreter to just silently quit.
As an example I have installed numpy in the following way
py -m pip install numpy
And then running Python through PowerShell:
PS C:\Users\USER> py
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
PS C:\Users\USER>
As soon as I run import numpy the python interpreter simply silently returns.
I have tried to install every python 3.6.x available from Pythons official website, and the issue persists.
I fully removed old versions of python between each install.
I cannot find any error logs or anything.
What can be the issue here and how can I debug further?
Any internal library (that I have tried) works
I have tried with the following libraries:
Numpy
Tensorflow
Protobuf
matplotlib
scipy
pandas
nose
sympy

I don´t know what version of Python I have, and problem coding my first website

I have installed the latest version of python (3.7.3)
When I go to cmd and I put py, it says my version is 3.7.3. But the problem comes when i put python or python --version, because it says that my version is 2.7.10. And i don´t know why.
I´m learning how to do my first website, and i´m just following what the tutorial says. I don´t know if the version of python is the cause of the problem, but i tell you what happens to me appart from not knowing what version i have.
I´m using Visual Studio Code and the guy from the tutorial said that we had to have at least a version of python over 3, and to find this out we have to write on CMD python --version. Then we had to know what version of pip we have putting pip --version.
Now I´ll show you the code he did and I copied, I had to install flask from CMD putting pip install flask.
So when I go to CMD to run this as the youtuber did, putting cd Desktop then cd NoFear (NoFear is the name of the folder) and finally python index.py. I got the following output:
C:\Users\Usuario\Desktop\NoFear>python index.py
Traceback (most recent call last):
File "index.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
C:\Users\Usuario>py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\Usuario>python
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\Usuario>python --version
Python 2.7.10
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return 'Hello World'
if __name__=='__main__':
app.run()
Type Py -3 index.py in cmd instead of typing python index.py. I hope this helps
You have two versions of python so uninstall the older version first
You can create a virtual environment that will isolate your Python dependencies. Because you have stated you are using Python 3.7.3, you can create a virtual environment by running the following command:
$ python3 -m venv .venv
Then you can activate it by using:
$ source .venv/bin/activate
Then if you run python in the terminal, you will start Python 3.7.3 because your virtual environment has been created using this version of Python.
Finally, if you want to deactivate the virtual environment just run:
$ deactivate
You can uninstall a your other python versions or if you want to keep them you can rearrange the path. That being said I would recommend looking into creating virtual environments.
1) In the bottom left of windows search for: environment variables
2) Select Environment Variables... on the bottom right.
3) Select the Path variable on the top (or bottom if there is not one on the top).
4) Move python3 above python2.7 and save.
Maybe get atom cause you are working in IDLE and i had the same problem each time you want to use code form IDLE on other software you cannot run it because of the lines on top
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
you have to take them of or use virtual studio or atom or oder IDE, i think.

Python 2.7 Anaconda2 installed in windows

My Python 2 environmental path:
C:\Python27
C:\Python27\Scripts
My Python 3 environmental path:
C:\Python35
C:\Python35\Scripts
I set the environmental path for Anaconda2
C:\Users\User\Anaconda2\Scripts
C:\Users\User\Anaconda2
But when i typed python to enter the shell in cmd (C:\Users\user)
Importing the module of Anaconda like numpy or matplotlib
C:\Users\User>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named numpy
C:\Users\User>cd Anaconda2
C:\Users\User\Anaconda2>python
Python 2.7.12 |Anaconda 4.1.1 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import numpy
>>>
So i don't know
1.Why my module can't import while not in Anaconda2
2.It is said that the path of Python2 will overrdie the Python,so how to enter in the Python35 shell?
thanks everybody
Each Python installation has its own libraries. As you will see, you are not running the same Python 2.7 interpreter when you run with Anaconda active as you are without (I assume that's either the system Python or one you installed yourself).
Libraries installed in one interpreter aren't available to others. You should consider learning about conda environments to allow you to manage multiple projects easily.
The command deactivate should terminate the Anaconda environment, and if the Python 3 interpreter is first on your PATH you should then be able to run it. Another way would be to explicitly use the command
C:\Python35\python
which should work even with Anaconda active.
Caution: it's a long time since I used Windows, and I don't have current information on getting multiple Pythons to live happily together on Windows.

Why would a Python import succeed when invoking *.py file from python but not when invoked by file name alone

I'm in the early stages of integrating some Python to invoke some computations in Matlab and roll up all the figures into some html generation I'll be doing in ll.xist. I had initially installed Python 2.7.5 32bit, but with a 64bit Matlab R2015a installation I could not install the matlab engine for that version of python. I then downloaded a 64bit version of Python 2.7.9 and tried to do a pip into ll-xist, which was apparently not well maintained and failed. I then downloaded Python 3.4.3 64bit, downloaded the ll.xist installer ll-xist-5.13.win-amd64-py3.4.exe and thought I was good to go. I had to do a pip to pull in cssutils, which succeeded.
Now I come to the point where I invoke my python file from the console. If I invoke it preceded with python everything works fine, if I don't the import isn't recognized. What could account for that discrepancy? Looks like some installation snafu between the various versions I've installed, somehow the python version on the path isn't being called appropriately and I'm guessing the 2.7.9 version of python is being invoked somehow because that install never had ll.xist installed, though my 2.7.5 install did.
C:\Temp>python MyScript.py a.txt b.txt
file1: a.txt
file2: b.txt
C:\Temp>MyScript.py a.txt b.txt
Traceback (most recent call last):
File "C:\Temp\MyScript.py", line 20, in <module>
from ll.xist import xsc
ImportError: No module named ll.xist
Here's a sanity check too...
C:\Temp>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\Temp>which python
/cygdrive/c/Python34/python
Check the file association for .py. While the proper installation might be in your path, the file association might still be pointing to a different version.
You can check this by importing the sys module in a test script and call print(sys.version). Then run the test script with python test.py and just test.py to see which versions are printed.
Turns out the governing issue here is what is known as the Python Launcher for Windows.
3.4. Python Launcher for Windows New in version 3.3.
The Python launcher for Windows is a utility which aids in the
location and execution of different Python versions. It allows scripts
(or the command-line) to indicate a preference for a specific Python
version, and will locate and execute that version.
From my console, I can see the "default" version via the py command...
C:\Users\me>py
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\Users\me>py -3
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Here's the fundamental fix...
The key benefit of this is that a single launcher can support multiple
Python versions at the same time depending on the contents of the
first line.
The first line I had to add in my case to my script was
#! python3
Great commentary linked here for details and motivations behind the Launcher.

How to run programs in python2 and python3

I have python 2.6.6 and python 3.1.3 currently installed on my machine (Windows Vista 64 bit)
My path variable includes the directory of both versions. How can I specify which python I want to run a program in. For instance, if I want to run a program in python 3, it works but if I want to run a different program in python2 I get a syntax error. So how can I run a python 2 program in the cmd?
Typing python in my command line, python 3.1.3 is the only one that shows up.
You can specify the version in the executable name python2.6 and python3.
Instead of just typing "python" on the command line, use the full path the python.exe that you want to run: FULL_PATH_TO_PYTHON_2.6.6\python.exe or FULL_PATH_TO_PYTHON_3.1.3\python.exe should distinguish between the two.
The Python Launcher is probably what you need. I used it with 2.7 and 3.2.
You can also use: 'py -main_version script_name.py args'
Example:
py -2 script_name.py args for Python 2.X
py -3 script_name.py args for Python 3.X
To test both are working or not, you can try,
>> py -2
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>> py -3
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
The shell will read the PATH from left to right, so you most likely defined Python 3.1.3 before Python 2.6.6. Specify the full path for each to use both versions.
If someone is using jupyter, and you have both python installed, you can also select which kernel to use

Categories