Visual Studio Code not opening any python interpret - python

I am having an issue with opening and running python on VS Code. In particular, I cannot open any python interpret regardless of having python installed on the computer and the python extension in VS code.
1 week ago, there was no issue with that, I opened VS Code, followed the instructions and python was running (however without any packages or libraries). Because I use python for Data Science projects, I decided to integrate Jupyter notebook - here again, no issues.
Afterwards, I got the idea that I should be able to import libraries and run codes in file_name.py, not only file_other_name.ipynb, so I was trying to create an environment in my project folder that will store installed packages by typing py -3 -m venv .venv .venv\scripts\activate and python -m pip install matplotlib in the terminal. Unfortunately, that did not work when running a basic script - getting a message "Activate.ps1 is not digitally signed. You cannot run this script on the current system."
Hence, I set up the powershell to Set-ExecutionPolicy -ExecutionPolicy Unrestricted. Once, I restarted VS Code, the current environment started showing a message 'Select Python Interpret'. I tried to reinstall the python extension and select from the Command Palette (Ctrl + Shift + P) any of the python interprets, that it is giving me but nothing happens. The message does not change regardless of how many times I have specified which interpret to open. Any idea how I can open the python interpret once again?
enter image description here

There's a default setting called "python.terminal.activateEnvironment": true, so every time you create an virtual environment and select it as your interpreter, don't forget to open a new integrated Terminal(Ctrl+Shift+`) to activate it.
[EDIT]
When you open a new integrated Terminal, there'll be an statement executed automatically: that's a file in your created .venv, which is by red underlined in the following screenshot. Then you get a virtual name prefix before PS, that means you've activated it successfully.
If the Activate.ps1 isn't executed automatically, turn to the .venv folder, copy its absolute path then run it in Terminal still can activate the environment.
Then go on development, like installing the required module:

Related

Python script runs from terminal in virtual environment, but fails to find pandas with VS Code play button

I am new to VS Code. I have created a virtual environment and selected that as the interpreter, in which I have run
pip install -r requirements
from the terminal which allowed for pandas to be installed in that virtual environment (I believe based on the docs
From that directory in the terminal, I ran
python myscript.py
It works. Generates the output as it should.
I then try to run the same script in the window above and I get the error, no module named pandas. I have not changed the interpreter.
UPDATE
When closing the terminal and then attempting to run the code from the explorer/deug window, I received a failure to run scripts notice. I therefore (based on this post changed my Execution Policies in Windows Shell (separate, not in VS terminal), and now it at least goes into the environment, however I still get the same error, pandas unknown.
UPDATE 2
I noticed in the tutorial that they created the directory and then created the environment within that. I moved my code to be under the virtual environment I created and then it works, both from the terminal and from the debug/explorer window. However here I noticed that the intrepetor is not characterised as a virtual environment but instead looks like the standard version- now there is no option for me to select working_env as an interpretor.
Running pip freeze shows the latest version of pandas which is not what I have installed elsewhere for instance. So this works, but I am now not sure if this is really how I should be using virtual environments.
**UPDATE 3
For whatever reason I can now update the interpreter. When I do however I am back to where I was. I can run the program in the terminal with the interpreteter selected, but running from the debug window I get the error that pandas is missing. Trying to install pandas I am told the requirement is satisfied. Running pip freeze confirms that this is installed. I have the feeling I am somehow installing pandas not in the environment I want
What have I missed in my vscode setup?
Per both the comments, I had a couple of issues here.
For whatever reason during update 2, I couldn't see the interpreter when I was in the folder initially but it was there. It is noted in the docs that this can take a while. Regardless, it must show in the bottom left that you are in the interpreter
Using powershell, I needed to change the execution policy to be unrestricted.
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Then I hadn't installed pandas in the virtual environment AND with the interpreter. Therefore
. ..\Scripts\activate.ps1
activated the working environment
python -m pip install -r requirements.txt
installed my packages
Running my script was achieved by
python .\myscript.py
and that also runs with the play button (though here I had to press Ctrl+Sft+P type Terminal: Select Default Shell, ensure the terminal was powershell & restart)

How to make VS Code update the Python autocomplete and installed modules caches?

VS Code will recognize available python packages and modules.
In this case, I started a VS Code session without a python package installed.
I then opened a terminal (in VS Code) and ran a pip install command to install the missing packages (the python interpreter for this project is set to a virtualenv that lives inside my project directory). However, VS Code still shows these packages as unresolved.
If I close and reopen VS Code, it picks up the new packages. Is there a better way of getting VS Code to automatically pick up these changes than restarting?
Probably similar to restarting but Ctrl+Shift+P > Reload Window should do it, without clearing your opened files

PyCharm not able to create conda environment

I'm pretty new to programming, so maybe there's something painfully obvious that I've missed, but I've searched a lot around for solutions to this issue without finding any.
I just got a new computer and installed Anaconda and PyCharm for Anaconda. I got it to work fine on my old computer, but when I try to open PyCharm now and create a project, it gets stuck on the creation of the conda environment. Apparently there's a new conda update, so I tried to update to this one in Spyder, which seemed to work fine, but when PyCharm tries creating a new environment, it gets stuck and only displays
"Creating Conda environment"
"$ conda update -n base -c defaults conda"
Does anybody know what's happening? I've tried just waiting it out, but it doesn't seem to be moving forward at all from that point. I've also uninstalled both Anaconda and PyCharm with all their plugins and reinstalling them, but to no avail.
Thanks for any help or input!
Sincerely, confused first year student
It appears that Sophie was able to get the conda environment up and running.
However, other newcomers might come across a similar problem with regards to setting up Anaconda for use within PyCharm. Therefore, I provide a guide below that I recently (today) used in order to accomplish this on a fresh install on a new computer. For this I decided to use PyCharm Community Edition 2018.3 and Anaconda3 version 2019.07. I performed this on Windows 10 Enterprise version 1903. I have also performed this procedure on Windows 7 Home Premium edition (some time ago).
This will enable you to use PyCharm and Anaconda together, so that you can:
Know the prerequisite steps leading up to creating a conda environment, and become familiar with the steps following this action with using Anaconda with PyCharm.
Use the Terminal window in PyCharm with packages that are installed with Anaconda's version of Python. You can then use both regular python and conda commands to view, update or install packages. For instance, depending on your preference, you could use 'pip list' (python command) or 'conda list' to list installed packages.
Use the Anaconda python interpreter as the default Python Console in PyCharm
To accomplish this, please follow these steps:
Install PyCharm, and import any settings file you may have from previous PyCharm installation on other computer (optional). Assuming you are starting fresh, create a new project and set the (default) project folder location.
Install Anaconda. My install location was set to C:\ProgramData\Anaconda3. Note that 'ProgramData' folder is hidden by default. You should enable viewing of hidden folders in windows explorer, if you need to manually browse for it. Upon installation, I decided to have the options Add Anaconda to my PATH environment variable and Register Anaconda as my default Python 3.7 set as deselected.
Open Windows Explorer and paste in C:\ProgramData\Anaconda3 (or your install directory) in the address bar. This should take you to that directory. Navigate to the environments folder named envs. Select the address bar and copy that address. I got C:\ProgramData\Anaconda3\envs. You will need this later.
Open program named Anaconda Navigator (Anaconda3). You should see this from the list of newly installed programs. Alternatively, search for it and open it. This will initialize some stuff (unknown to me) that I have found necessary to be able to complete this guide. For the next steps we dont need this program running. You could close it if you want.
Decide on a name for the (ana)conda environment that we will create. I used py37 for simplicity. You only need to do this once for the python projects you will make. It is possible to make more environments if you should need that for some python projects in the future.
Open program named Anaconda Prompt (Anaconda3), preferentially as administrator. Opening as administrator may prevent complications that could occur on some systems. To do so, search for the program and then right-click it, and choose Run as administrator.
Enter this command: conda create -n py37 python=3.7 anaconda. It should prompt you to install some packages. Enter y to accept and proceed with the setup. This step may take some time to finish. Once finished, it may show you the needed command to activate the environment. Don't activate it just yet, ie. ignore that message for now.
Assuming environment path C:\ProgramData\Anaconda3\envs and environment name py37, run the following command (alter as needed to match your environment path and name): set PATH=C:\ProgramData\Anaconda\envs\py37\Scripts;C:\ProgramData\Anaconda\envs\py37;%PATH%
Run this command: conda activate py37. This will activate your environment.
Navigate to a folder you can easily access, such as the Documents folder on your machine. To do so, execute this command: cd "C:\Users\YourUsername\Documents".
Execute the following command: echo %PATH% > path_value.txt. This will export "PATH" values to a text file that can be found in the Documents folder as path_value.txt. Open this file and copy the content within. This will be needed in the following steps with setting up PyCharm for use with the Anaconda environment.
In PyCharm, navigate to Settings - Tools - Terminal. Check that Start directory field contains the path of your project folder, ie. such as C:/Users/YourUsername/Documents/YourProjectFolderName. Then, select the folder icon for the Environment variables field. Click the plus symbol, and add a new entry with PATH and your path value (as found from the path_value.txt file) in the Name and Value fields, respectively.
In PyCharm, navigate to Settings - Project Interpreter - Add Python Interpreter - Conda Environment - Existing environment. Browse for C:\ProgramData\Anaconda3 (or your install directory). Select the file named python.exe. I chose to edit the Name field of this Anaconda python interpreter as Python 3.7 Anaconda.
In PyCharm, navigate to Settings - Build, Execution, Deployment - Console - Python Console. Check that the Python interpreter field contains Project Default (Python 3.7 Anaconda) or similar entry. The Environment variables input may be empty. Select the folder button next to it and add a new entry with PATH and your path value (as found from the path_value.txt file) in the Name and Value fields, respectively. Exactly the same entries as was inputted in step 12.
[Optional] In PyCharm, navigate to Settings and search for the Run context configuration option. Right click it and select a desired shortcut method. Personally I use a keyboard shortcut which is set to Ctrl+Shift+Less. Once the rest of these steps are followed, this will allow you to run the current python .py file you have open, without having to manually set up configuration options for it (through Add Configuration option, next to green play button, as seen if you have no configurations set up from before). It will force PyCharm to use the default project interpreter (which now is the desired Anaconda-delivered Python 3.7 installation) for whatever project you have open. To use it, just open a .py python file with some code inside, click anywhere inside of it, and then use your shortcut option.
[Note: If you skipped step 15, go to step 17] Run your python file with the method from step 15. This should automatically set up a run configuration option for that file. You may get an error of missing packages. Ignore that for now.
Select Edit Configuration or Add Configuration in the dropdown box next to the green play / run button. Open the Templates tree, then select Python. Check that the Python interpreter option contains Python 3.7 Anaconda or similar. The Environment variables field may contain PYTHONUNBUFFERED=1. Regardless, select the related folder button and add a new entry with PATH and your path value (as found from the path_value.txt file) in the Name and Value fields, respectively. Exactly the same as we did in steps 12 and 14.
Restart PyCharm to re-initialize the Terminal and Python Console windows. Or manually close and reopen those windows.
You have now created and activated an (ana)conda environment, and enabled it's use from within PyCharm. You should also now be able to ie. copy your code over to a fresh new file, and then hit ie. Ctrl+Shift+Less to run the file without having to manually set a configuration file for it.
Hope this helped!
Sources: source1, source2, source3
In my Experience I just opened PyCharm as administrator and the error was gone!
If you use PyCharm to create conda environment, you can choose conda environment not virtualenv.
When you get the error like "can't get the /path/to/", you can open PyCharm with administrator.
If you use "anaconda prompt" to create conda environment, you can use command line like conda create -n py36 python=3.6. Then you can activate environment using conda activate py36.

Python 3 ModuleNotFoundError: No module named 'numpy'

I trying to run a program in python 3 that uses numpy but it gives me the error ModuleNotFoundError: No module named 'numpy'. I am using Windows 10. I tried running pip install numpy and it says pip is not a recognized command
You need to add python to your environment variables
Computer -> System Properties (or Win+Break) -> Advanced System Settings
Click the Environment variables button (in the Advanced tab)
Edit PATH and append ;C:\Python27 to the end (if you need substitute your Python version)
Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
If you are running python 3.6 you will need to add python to your environment path through command prompt
Windows allows environment variables to be configured permanently at both the User level and the System level, or temporarily in a command prompt.
To temporarily set environment variables, open Command Prompt and use the set command:
set PATH=C:\Program Files\Python 3.6;%PATH%
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
These changes will apply to any further commands executed in that console and will be inherited by any applications started from the console.
To change System variables, you need non-restricted access to your machine (i.e. Administrator rights).
If you were to run python -v it should now work
This came directly from the python documents section 3.3.1
Now after you restart you should be able to access python through the command line. If you are running 2.7.9+ or 3.4+ pip will come prepackaged with python. However, if you are running an earlier version of python
Per https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip:
Download get-pip.py, being careful to save it as a .py file rather than .txt. Then, run it from the command prompt:
python get-pip.py
You possibly need an administrator command prompt to do this. Follow Start a Command Prompt as an Administrator (Microsoft TechNet).
Thanks to How do I install pip on Windows?
Add "way to folder with your python interpreter" and "way to folder with your python interpreter"\Scripts\ to PATH variable. Computer -> Properties -> Extra options -> Environment variables.

Using linux console in pycharm

I'm new to pycharm, virtualenv, linux and git.
I've recently begun a journey of using djangoto make webapps. Before I used sublime to make scripts however now a more complex project management system such as pycarm was needed. I actually wanted to get a linux VM and go down that road but was advised that windows python IDE such as pycharm would be suitable
I recently learnt the importance of dependencies and how to use virtualenv. However in this tutorial, under the 'How do I use my shiny new virtual environment?' it starts using commands such as:
ls env
and
which python
Neither of which my pycharm console would understand.
I could use a console emulator such cmder to use the commands but then I would remove the convience of using the IDE's integrated one.
Should I upgrade to a linux VM ? Or can I install a package that allows me to use such commands in PyCharm.
As a bonus question, what are the commands in that tutorial ? are they linux commands? when ever i see $ .... is that the linux console ?
You can accomplish this using Vagrant: https://www.vagrantup.com/
You can use Vagrant and VirtualBox to setup a Linux VM (distro of your choice) and then install all of your Python dependencies in the VM. Once you have that setup, you can tell PyCharm to use the Python interpreter in your VM by following these steps:
Open the project settings dialog box in PyCharm.
Expand Project: (your project name) on the left side.
Click on Project Interpreter.
Click on the cog icon on the upper right side of the window and select Add Remote.
Click on the Vagrant radio button.
In the Vagrant Instance Folder box, select the directory your Vagrantfile is located in.
In the Vagrant Host URL box, make sure ssh://vagrant#127.0.0.1:2222 is specified.
Click OK.
Since Vagrant is compatible with Windows this solution should work for you. I have done it successfully using macOS and it works great. Good luck!
You might find this tutorial useful: https://developer.rackspace.com/blog/a-tutorial-on-application-development-using-vagrant-with-the-pycharm-ide/
I got this to work on Windows 10 with Anaconda Prompt. This terminal which comes with Anaconda, creates a "base" environment with a linux-like virtual machine and your Windows file system (C:\\) mounted to /c, and has bash installed with common Unix commands like cd, ls, chmod, echo, cat, ... Running programs from bash with access to environment variables is much nicer than Windows Powershell etc.
Now to get your Terminal in Pycharm to use Anaconda Prompt instead of cmd.exe, I followed this answer. After installing Anaconda and/or Anaconda Prompt, right-click -> Open File Location -> right-click the shortcut -> Properties -> copy file path. Then use your file path instead.
Conda is great for package environment management. Learn more about it here. For Django + Conda specifically, read here. You can also use pip to install from Python package indexes, github repos, and requirements.txt files instead. Unless you know how Anaconda Prompt works, I don't recommend creating your own environments from scratch. What worked for me was:
(base) C:\Users\wassadamo> conda create -n mynewenvironment --copy base
...
(base) C:\Users\wassadamo> conda activate mynewenvironment
(mynewenvironment) C:\Users\wassadamo> ls
folderA folderB file.txt
Works!
Whenever I try running conda deactivate to leave the base environment, my bash commands would stop working. So clone base as above.
Another tip: if you want to run shell scripts from Terminal within PyCharm with Anaconda Prompt this way, then execute them (e.g. "run.sh") on command line with
bash run.sh
I tried putting this on the first line of my run.sh
#!/usr/bin/bash
And running it with
./run.sh
But this had the effect of running it in an external Anaconda Prompt instance (add sleep, or some user input command to force it to wait and see for yourself). Explicitly running my .sh files with bash had the desired effect of running them in the same shell as I started them in PyCharm Terminal configured with Anaconda Prompt.

Categories

Resources