Importing module to VS code - python

im very new in programming and i learn Python.
I'm coding on mac btw.
I'd like to know how can i import some modules in VS code.
For exemple, if i want to use the speedtest module i have to download it (what i did) and then import it to my code. But it never worked and i always have the error no module etc.
I used pip to install each package, i have them on my computer but i really don't know to import them on VS code. Even with the terminal of the IDE.
I know it must be something very common for u guys but i will help me a lot.
Thx

Quick Summary
This might not be an issue with VS Code.
Problem: The folder to which pip3 installs your packages is not on your $PATH.
Fix: Go to /Applications/Python 3.8 in Finder, and run the Update Shell Profile.command script. Also, if you are using pip install <package>, instead of pip3 install <package> that might be your problem.
Details
Your Mac looks for installed packages in several different folders on your Mac. The list of folders it searches is stored in an environment variable called $PATH. Paths like /Library/Frameworks/Python.framework/Versions/3.8/bin should be in the $PATH environment variable, since that's where pip3 installs all packages.

Most probably you have multiple versions of python on your computer.
You have to select your python interpreter for which you have installed those packages using pip to do this you'll have to click on the python version you see in the bottom and select the correct interpreter from the list:
You can also find more information within the VSCode docs.

Related

Vscode - can't import numpy but pip says it is installed?

I'm writing some python in a venv but it won't let me import numpy even though it is installed...
I can see in the venv folder that PyQt and NumPy are BOTH installed and in the library, and pip tells me this as well, but it doesn't let me import numpy, even though PyQt, which is in the same venv library, is able to be imported into Vscode!
It even works when I import it in the shell, but not in the actual class...
Also, numpy is not only installed in the venv, but it is also installed on my actual system as well.
Does anyone know what may be causing this?
According to the information you provide, it is recommended that you refer to the following two aspects:
The use of python environment.
Please use the command "pip --version" or "python --version" in the VS Code terminal to check whether the python environment used by the current terminal is the same as the python in the lower left corner of VS Code.
(If they are not the same, please use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, it will automatically enter the selected environment.)
In addition, for virtual environments, please make sure that the terminal has activated this environment.
Please check the installation package of the module. You could use the command "pip show numpy" to view the location of the module "numpy" in this environment, then find the module folder and delete it, and then reinstall it, so as to avoid the damage or incompatibility of the internal files of the module. (The modules recognized by VS Code are stored in "lib\site-packages" in the python environment used.)

PyCharm projects uses each others pip and python files

I am working on several projects on the same PyCharm. Like I "attached" them all together. But I recently noticed some weird behaviors. Like when I import a library I haven't installed yet to my script. It shows me a little error as expected. But when I try to install that using python -m pip install my_library, it tells me that it has already installed. I recently noticed that this is because it's using and other pip from another project. I doesn't use the one in the venv folder in the project. Also to run the scripts sometimes it uses python.exe from pythons original directory. It's a whole mess and I have no idea how I can solve it. Sometimes my projects requires different versions of the same library and you can imagine what happens when I change the version.
I make sure each project is using their own interpreter. Don't know what else to do other than this. I am using Python3.6.4 PyCharm2018.3.2 running on Windows10
it sounds like all your projects are configured to use the system's interpreter instead of the virtual environment you set up for each of them.
Follow this instruction to fix it https://www.jetbrains.com/help/pycharm-edu/creating-virtual-environment.html
In terms of using different version of the python library, you can address that by specifying it in requirements.txt file, which you can put in your venv folder for each project. then you can just do pip install -r requirements.txt after you set up your venv. (you need to ensure that the venv is activated - you don't need to worry about this if you have configured the project in PyCharm to use the venv's python interpreter.) You can check this by going to Terminal in your PyCharm and you should see (venv_name) hostusername#host:~/project_folder$

Python modules not importing

New to python and I cannot import modules that I have installed via pip.
For instance, I have installed numpy though cannot import it.
I have a feeling from trying to work this out that it is installing to the wrong directory, or I am calling the wrong version.
$ which python
returns
/usr/bin/python
I am just not sure how to change it so I can access the modules.
First if all, you are installing the packages using pip, which means you install it on python 2 by the default configurations.
The issue you are describing can be caused by several problems:
If you are working with an IDE like pycharm- your project interpeter might by python 3.x. You should change it to python 2 since you used pip and not pip3.
Some newer versions of pycharm are opening virtual environment by default in new projects. This means that if you install packages outside the virtual environment you will not be able to access them. When opening a project, intended of applying the default settings, change the interpreter to your system interpreter, probably your python2.7 in your case.
You are not using an IDE but accecing python from your terminal like so: python3 instead of python.
Hope it helps ;)
From a terminal try:
pip install numpy --user
This install numpy to your home directory. Sometimes this helps compared with installing it without the '--user' flag.
Then:
python
Now you have a python command line. Try:
import numpy
If you don't see any error message, then the install worked. Control-d or typing 'exit()' returns you to your shell.

How can I run python version 3.6.2 instead of 3.6.1

this is my first time here, and I'm fairly new to python, so please let me know if you need more information. Thanks in advance.
I am running python 3 on Windows 7
I discovered my problem after I used pip install numpy. This works just fine. Then, when I try to use import numpy in the python shell I receive the ModuleNotFoundError: No module named 'numpy'. That's when I noticed that my default version of python was 3.6.1, despite having updated to 3.6.2 at some point. I still have both .exe setup files and when I run them it shows that I only have 3.6.2 installed. However, when I type python --version in the command line i get Python 3.6.1, even though Python36-32 is what i have in my path.
I think my question is how can I make sure I'm running the newer version of python as my default, or if need be, how can I get rid of the older version?
When you type python in cmd, it searches python command inside the directories in the environment variable named Path. Actually Path includes both python directory and python scripts directory. For example, in my computer, Path includes:
C:\Users\user\AppData\Local\Programs\Python\Python36
C:\Users\user\AppData\Local\Programs\Python\Python36\Scripts
pip is in the python scripts directory. Your Path can be wrong. You should check it. This link can help you. You should have one python directory and one python script directory in Path, just the version you need.
Also, you can call pip as a module:
python -m pip install numpy
This will install the package to the version which is in the Path, Python 3.6.2 in your situation.
If none of these works, I recommend you to uninstall(delete) Python 3.6.1, and try to use pip again. If pip doesn't work(or disappeares), you can read this or use get-pip.py to install pip to your computer again. Maybe, you can delete all python versions, and install the version you need, and of course, you should be careful about Path again.
EDIT:
I am not sure about your problem. Some informations are needed for a certain solution.
You can find the source of an executable(python or py in your situtation) with where command. Here is an example from my local:
where python
Output:
C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe
That means C:\Users\user\AppData\Local\Programs\Python\Python36-32\ directory is in my Path and when type python, cmd runs python.exe.
So, you can find out what are py and python exactly.

I installed pydicom through pip but it somehow cannot be found when I try to import the library. Could someone give a suggestion to fix this?

Please see this screenshot:
Isn't the package pydicom already installed?
Credit to Igor:
It looks like that it's working in Sublime Text 2's build using "import dicom". However it is still somehow not working in my Eclipse with PyDev environment.
***I have solved the problem in Eclipse after adding a path in the Python Interpreter configuration by hand (Somehow Auto-Config didn't add this path.).
I did the same series of steps and saw the same issue. After digging around it looks like the package appears as "dicom". Try this instead:
import dicom
Edit: Looking at this question mentioning this library, everyone is importing it this way too.
try:
python -m pip freeze
Is the package there? If not, you have installed it for the wrong python environment. I suggest you install it as follows:
In Bash prompt (terminal):
python -m pip install pydicom
You can change python with python3 or the absolute address of the executable. If you don't know the absolute address of the executable, you can obtain it as follows:
In Bash prompt (terminal):
which python
(or python3). The output would be the absolute path, which you may utilise like so:
/Users/xyz/bin/python -m pip install pydicom
Finally, if you want to find out the path to your executable from within Python, you may do so like this:
from sys import executable
print(executable)
The output will be the absolute path to the current environment's interpreter.

Categories