I am running VSCode on Windows 10. I've set up a virtual environment and have installed a number of packages to the local site library.
I've activated my environment (The terminal prompt shows a .venv string)
However, when I attempt to import any of my local modules, I get an 'Module not found'
error.
Doing a pip list shows that the modules do exist in the virtual env.
I've verified that I'm running the Python executable in the virtual environment.
Printing sys.path gives the following output:
['', 'C:\Users\User\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\User\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\User\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\User\AppData\Local\Programs\Python\Python39', 'C:\Users\User\Documents\mednotes\.venv', 'C:\Users\User\Documents\mednotes\.venv\lib\site-packages']
The AppData path is, I believe the global Python namespace. Why is this even in my
sys.path in my local virtual env? I added the last two paths manually to see if this
would fix anything but no luck.
I'm really stuck here. Anybody have any suggestions for fixing this?
Thanks
Are you sure pip installed them to the correct place? Can you see the packages you installed under the C:\Users\User\Documents\mednotes\.venv\lib\python3.9\site-packages folder?
I would double-check where python3 and pip are getting picked up from. Try where python3 and where pip.
A virtual environment is a built-in way to create an environment to isolate the packages you install per workspace.
When you use a virtual environment, it will isolate the local package.
You can use shortcuts "Ctrl+Shift+P" and type "Python: Select Interpreter" to choose the correct interpreter.
Another way is to use conda install packageName command to install the package in the virtual environment.
Related
I have a python installation on my pc (windows 10) which comes from Anaconda. I am a data scientist and using conda as a package manager is very convenient for me.
However, sometimes I want to develop a small app or script to share with my colleagues. In these cases I create a project folder and python -m venv .venv inside it.
This way, I can install only the essential packages I need, and later share the requirements.txt file.
The issue I'm having, is that the python interpreter being used is still the default one, namely, the one that came with Anaconda, even if I activate the virtual environment and deactivate the conda one.
Specifically, if I run python in the terminal, I get this warning message:
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
This is rather inconvenient. My base python installation is 3.7, but if I wanted to use an earlier version, or 3.8, I can't seem to be able to choose.
I would expect that the python executable being used is the one in the current active environment, but this doesn't seem to be the case.
How can I obtain that?
First you have to install the version of python you want to use in your venv. It must already be available somewhere on your system to create a venv using it.
Then instead of just python -m venv .venv you specify which python with the full path : /path/to/pythonX.Y -m venv .venv
You can't have a venv that shares multiple versions of python, to my knowledge at least.
I am fairly new to using virtual environments for python projects but I have spent many hours trying to resolve this issue I've been having through various posts on this site and on others and have come up short. I have even resorted to uninstalling all python distributions and reinstalling with no luck.
I have the anaconda distribution of python installed on Windows 10. I have the problem that after I create a virtual environment, called venv, in a project folder I am unable to then activate this virtual environment using the anaconda prompt to install packages through pip. What I have done so far is as follows:
(base) C:\Users\[User]\Documents\GitHub\[project_folder]> virtualenv venv
This then successfully creates a virtual environment folder in my project folder called venv. Working in this directory I navigate to:
(base) venv> cd Scripts
(base) venv\Scripts> activate
There are a number of activate files to choose from and neither turns the environment from base to env as expected.
The reason I am using anaconda prompt as opposed to the standard command prompt or Windows PowerShell is because when I'm using the pip install <package> anywhere but in the anaconda prompt I get an error message that reads:
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
I'm not sure what this means and have tried to do some googling to solve this issue but to no avail. Note I do not have this issue when working on python projects on my MacBook and I can easily enter virtual environments and install the packages there with no issues. I'm relatively new to Windows so perhaps I'm missing something here...?
An interesting observation is that I can enter any virtual environment I've created through Windows PowerShell but I encounter the pip problem described above when trying to install packages into this environment.
I'm desperate for some help resolving this issue as it's greatly hindering the work on all my projects, and unfortunately I cannot just use my MacBook to code either.
use those command for python virtualenv
For activate
WINDOWS: activate nameofvenv
LINUX, macOS: source activate nameofvenv
For deactivate
WINDOWS: deactivate
macOS, LINUX: source deactivate
I am trying to create a new python 3.7 virtual environment on my local computer running Windows 8. I have python versions 3.6, 3.7, and 3.8 installed. Their exe's are named python36, python37, and python, respectively. All three are correctly added to PATH because I can enter each interpreter.
Within my new project's directory I tried to create a virtual environment with python37 -m venv env. It produced an error: Error: [WinError 2] The system cannot find the file specified, but it still created the directory. However the Scripts subfolder is empty except for pythonw.exe.
In this question someone suggests doing python37 -m venv env --without-pip. When I tried this, the activation/deactivation scripts were created, but the virtual environment is using python 3.8.
It is my understanding that venv will create the virtual environment with what ever python exe you use to call it, so I don't understand how this can happen. I've verified that python37 points to the correct place with where python37, and can even enter the 3.7 interactive interpreter.
The problem was that I renamed the python exe's. I don't know exactly what goes wrong, but presumably at some point venv tries to find python.exe and is thrown off by the name.
Changing them back to python.exe and differentiating between the versions with their location fixed the problem.
Edit:
Check out Eryk's comments for more details.
First create folder at any drive then go to that folder and install virtualenv package using pip.
pip install virtualenv
Then create your virtual environment.
mkvirtualenv myvirtualenv
Then use below command to activate virtualenv in windows.
myvirtualenv\Scripts\activate
After this you can install related package in current virtual environment.
The Python Standard Library for Creating Virtual Environment
I would like to use Anaconda and the newest Pycharm 2016.3 together. I would like to be able to manage packages in settings->interpreter. If this is not supported, I would like to know the workflow of using these two together. According to another SO question, Pycharm 5 used to have a 'Create conda env' in the interpreter settings, but this seems to be gone now. I have tried:
1) Manually creating a virtual environment with 'conda create --name project numpy' and I add the interpreter ('~/anaconda2/envs/bin/python', the location of python for my created virtual environment. However, pycharm doesn't allow me to add any packages through settings->interpreter. Running an 'import numpy' through the console shows errors that are pointing to /usr/bin/python, not my virtual env python, and an error 'ImportError: cannot import name multiarray'. I'm not sure what package to add using conda from the cli, and the pycharm frontend doesn't add packages
2) I've tried the same as 1) but with my global anaconda python as the interpeter ('~/anaconda2/bin/python') and it doesn't seem to be able to connect to the console.
3) Creating a virtual environment through pycharm directly.
I would like to remove my default pythons (/usr/bin/python2.7/3.5 from the list of interpreters in pycharm) for debugging purposes but it won't let me and it seems to be showing packages that my anaconda virtual env doens't have installed.
Is there a way to manage my VIRTUAL enviornment in Conda using pycharm? If not, what steps do I take to make these two play well together assuming I can't manage it through pycharm interepreters settings.
This is the normal steps that i follow when i use virutalenv with PyCharm
I normally work on ubuntu
First, i always create a separate environment for every project using the command virtualenv "environment_name" from the command line.
Activate the environment using the command - source environment_name/bin/activate in ubuntu.
Suppose if i want to start a django project, i create the project using the command django-admin startproject project_name
Open this project in pycharm.
go to settings-> interpreter in pycharm. choose "add local" interpreter from the settings. It will open a pop-up. Go to the directory of the environment you just created and select the correct python interpreter you want to use.
now if you want to install a new package, you can go to interpreter settings and add package from the pycharm or you can fire up the command line, activate the environment and run pip install package_name. Once the package is installed, it will also show in pycharm.
if you are using Windows OS, use powershell to execute the above commands. The only difference will be in activating the environment. In windows, to activate an env use environment_name/Scripts/activate
EDIT:
Same goes anaconda environments also, the easy way is to manage the environment from the terminal and pycharm will show the packages changes in the interpreter settings.
Here is what I have been doing and it works great. I create a virtual environment separately and when I create a new project in PyCharm, I point the interpreter to the python from the virtual environment. An example is shown in the picture below:
While working on a new python project and trying to learn my way through virtual environments, I've stumbled twice with the following problem:
I create my virtual environment called venv. Running pip freeze shows nothing.
I install my dependencies using pip install dependency. the venv library starts to populate, as confirmed by pip freeze.
After a couple of days, I go back to my project, and after activating the virtual environment via source venv/bin/activate, when running pip freeze I see the whole list of libraries installed in the system python distribution (I'm using Mac Os 10.9.5), instead of the small subset I wanted to keep inside my virtual environment.
I'm sure I must be doing something wrong in between, but I have no idea how could this happen. Any ideas?
Update:
after looking at this answer, I realized the that when running pip freeze, the pip command that's being invoked is the one at /usr/local/bin/pip instead of the one inside my virtual environment. So the virtual environment is fine, but I wonder what changes in the path might be causing this, and how to prevent them to happen again (my PYTHONPATH variable is not set).
I realized that my problem arose when moving my virtual environment folder around the system. The fix was to modify the activate and pip scripts located inside the venv/bin folder to point to the new venv location, as suggested by this answer. Now my pip freeze is showing the right files.