Importing python package from conda environment in VSCode - python

enter image description here
Somebody please explain why I am not able to import package in VSCode even though I can do it in terminal both in VSCode and in system

Check out https://code.visualstudio.com/docs/python/environments
VSCode uses multiple python binaries. Each one has a seperate list of installed modules and packages. By default VSCode uses a custom Anaconda binary. When you invoke the python command, you are using a system wide install of Anaconda. You need to change the binary version of python VSCode is using, to point at the system-wide install, where you installed your modules

Related

Pygame not working on VS Code even though it is installed

I am struggling with getting pygame to work on VS Code. The only thing that seems to be amiss is that these are different versions and they are in different folders:
PS C:\Users\rebec\My Drive\VS Code\Python Config> python --version
Python 3.9.11
PS C:\Users\rebec\My Drive\VS Code\Python Config> pip --version
pip 22.1.2 from C:\Users\rebec\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pip (python 3.10)
I have tried uninstalling and installing pygame. I think it's something with the file configs but I just don't know how to fix it. Thanks in advance for the help!
You have two versions of python installed on your computer. One is 3.9.11, and another is 3.10. You installed pygame for python 3.10, and tried to run it with python 3.9.11.
If you want to run your game using the global interpreter (python 3.10, the one where you installed pygame), you will need to switch your interpreter inside VSCode:
Press Ctrl+Shift+P to open a command pallete.
Open Python: Select interpreter.
Set your interpreter to global one.
You might also want to consider using a virtual enviroment to avoid problems like these in the future:
Each virtual environment has its own Python binary (allowing creation of environments with various Python versions) and can have its own independent set of installed Python packages in its site directories, but shares the standard library with the base installed Python.

Python Libraries can't be detected

I installed some python libraries, but they can't be detected. In pycharm they are orange so excluted and Visual Studio Code says "Import "cv2" could not be resolved" if I want to include open cv. I also had 3 Versions of Python at first, but now I only have one left is that a problem?
Deleted old Versions of Python and Anaconda was the only thing that made the pip comand work again.
I looked up if everything is installed with "pip list".
You need to have the installed python in your path. The first interpreter it reaches in path will be used, if not just add it to your path
where.exe python
C:\dist\venvs\trk-fullstack-test\Scripts\python.exe
C:\dist\Python310\python.exe
C:\Users\mobj\AppData\Local\Microsoft\WindowsApps\python.exe
$ENV:PATH = "C:\dist\Python310;$ENV:PATH"
where.exe python
C:\dist\Python310\python.exe
C:\dist\venvs\trk-fullstack-test\Scripts\python.exe
C:\Users\mobj\AppData\Local\Microsoft\WindowsApps\python.exe
All modules installed with pip will follow the python interpreter used.
Recommend taking a look a https://docs.python.org/3/library/venv.html
to see how virtual environments are handled.

No module named 'numpy' in pycharm

I am quite new to python and pycharm.
I installed python 3.6 through anaconda, and I can see many packages including numpy are installed as I can see them in cmd (im using windows) by typing 'conda list'. Also, it works if i type 'import numpy' in python through window command prompt.
However, if I open pycharm and run "import numpy" there, it gives me 'No module named 'numpy' in pycharm'. May I know whats wrong with my setting? I guess it must be some problem with my interpreter setting.
I think my python is installed in C:\Users\AAA\Anaconda3\python.exe
I checked in pycharm, project interpreter is "C:\Users\AAA\PycharmProjects\untitled\venv\Scripts\python.exe"
Should I change it to the one under anaconda3 folder?
What is venv folder under "pycharmprejcts"? Is it a virtual environment? It shows (see the attached screenshot) the base interpreter is the one under anaconda? Should I choose to inherit global site-packages?
You should select Conda environment in Pycharm, not create a new, blank Virtualenv
Or at the very least - System interpreter, then find the Python executable for Anaconda
What is venv folder under "pycharmprejcts"? Is it a virtual environment?
Yes, it allows your project to be more portable - you define the minimum set of dependencies for your code rather than rely on everything installed only locally on your machine
You have a virtual environment, this helps keep the dependencies separate between projects. You can set your project interpreter to C:\Users\AAA\Anaconda3\python.exe or install the dependency in your venv, in pycharm you would press alt+enter on the import and press install.

How to set default interpreter and keep things in order?

I was required to install anaconda for a CS course and used spyder and Rstudio.
Then, for a different class I used pycharm.
When I type on the command line "python -V" I get:
Python 3.6.1 :: Anaconda 4.4.0 (x86_64)
and I have no idea why it relates the python version I have installed with Anaconda (and why not pycharm?). I understand that the OS runs python 2.7 (shouldn't I get that instead? and when I type python3 -V get which version of python 3 I have?) and when I use something like Pycharm or Spyder I can choose which version I want from the ones I have installed and use it within the program, not for the terminal.
I just want to have everything in order and under control. I don't think I understand what Anaconda really is (to me is like a program that has more programs in it...). How do I keep anaconda to itself ? 1313
Also, should the packages I installed through Terminal work on both pycharm and spyder/anaconda even though when I used pycharm I used python 3.5 and anaconda 3.6?
I think I need definitions and help to get everything in order in my head and the computer.
Pycharm is just an application to help you write code. Pycharm itself does not run python code. This is why in PyCharm, you need to set the interpreter for a project, which could be any python binary. In PyCharm, go to Preferences > Project > Project Interpreter to see where you would set the python environment being used for a given project. This could point to any python installation on your machine, whether that is the python 2.7 located at /usr/bin/python or a virtual environment in your project dir.
The industry standard way to "keep things in order" is to use what are called virtual environments. See here: https://docs.python.org/3/library/venv.html. A virtual environment is literally just a copy of a python environment (binaries and everything) so whatever directory you specify. This allows you to configure your environment to however you need in your project without interfering with other projects you might have. For example, say project A requires django 1.9.2 but project b requires 1.5.3. By having a virtual environment for each project, dependencies won't conflict.
Since you have python3.6, I would recommend going to you project directory in a terminal window. Running python -m venv .venv to create a hidden directory which contains a local python environment of whatever your 3.6 python installation. You could then set your project interpret to use that environment. to connect to it on the command line, run source .venv/bin/activate from where you created your virtual environment. run which python again and see that python is now referencing your virtual environment :)
If you are using a mac (which I believe you are from what you said about python2.7), what likely happened is that your anaconda installer put the Python bin directory on your PATH environment variable. Type in which python to see what the python alias is referencing. You can undo this if you want by editing your ~/.bash_profile file if you really want.
You are more or less correct about anaconda. It is itself another distribution of python and contains a load of common libraries/dependencies that tend to make life easier. For a lot of data analysis, you likely won't even need to install another dependency with pip after downloading anaconda.
I suspect this won't be all too helpful at first as it is a lot to learn, but hopefully this points you in the right direction.

Can I install Anaconda alongside Canopy?

I need to install Canopy, but I have Anaconda already installed. If I install Canopy will there be conflict or not? an if will be what are the possible problems?
Multiple Python installations can co-exist on a system, but it is important to keep them isolated (none should be set as default or referenced in any environment variable).
Canopy will not set these at all, so will not interfere with Anaconda. You can run Canopy Python scripts from inside Canopy. If you want to run them from a Terminal / Command Prompt, you can open it from the Canopy Tools menu.
To avoid possible interference of Anaconda with Canopy, please modify your PATH or PYTHONPATH environment variable to remove any references to Anaconda or any other Python installation using the instructions in this article as a guide (note that in this case you would be removing Python from this variable).
You may wish to write a small batch file or shell script to re-insert any such references temporarily when you do want to run anaconda.
I have not used Canopy but use system installed Python and Anaconda a lot so I can explain some issues people run into. When you have 2 different python installations there will be a problem of which Python is used(Type python at the command prompt and which one opens the interpreter?). Usually the executable Python location is added to the PATH so if 2 are in your PATH it will use the first one. With this you will likely have a mess with environments. If you go to use Canopy's Python you will not access Anaconda's Python packages and vice versa.
Other weird issues can come up if one python package picks up a .so or .dylib file that doesn't work or isn't the specific version. One installation may remove a version of these in favor of it's dependent version and then another piece of code no longer works.

Categories