Make a PyCharm project inheriting global site-packages, after creating the project? - python

I'm a newbie with PyCharm and Mac OS. I created a python project in PyCharm with VirtualEnv, and I didn't select the option of 'Inherit global site packages'. After working on this project for a while, I realized I need to inherit some packages that are installed globally.
There is a workaround here, which creates a shared Virtual Environment and uses it: Create shared virtual env. However, I wonder whether there is any way to make changes to current project, rather than creating a new project or creating a shared virtual environment?
A separate question: assuming that I have a project that inherits global packages. If after the project's creation, I installed other packages globally, will those packages available in this project?

I figured this out when clicking around.
In Pycharm, go to File -> Settings... -> Project -> Project Interpreter, click on the gear icon next to the menu:
Then choose "Show all...", and select the virtual environment you want to change, click this icon to add global packages' path into this venv's path
then click ok or apply to make it work. Usually, your global packages' path is "/path-to-python/Lib/site-packages", for my desktop is:
Now you can use all your global packages within the venv. For any system other than Windows, this should work fine as well.
But you won't see any global packages in pycharm's package viewer for this venv, which totally makes sense, because pycharm can only manager the venv for you, not the global one.
For your separate question: Yes. Because you only add global path into your venv, not like copying all package files into it. After adding the global path, all global packages should work inside your venv at any time.

Related

How to make pygame on available in an virtualenv? [duplicate]

Pycharm doesn't sync with the pip packages though i have added the path in environment variable.
The following elements will clear the topic:
already installed sklearn package:
pycharm can't find that package:
environment variable:
The thing is that pycharm creates all of your projects inside a virtual environment. So the packages that you installed outside the virtual environment(global site packages) does not get inherited in to your project. There is a simple solution for that. When you create a pycharm project, make sure to check the "inherit global-site packages" checkbox as shown here.Then you'll be able to import all the packages to your virtual environment, provided they have been installed in your computer.
Alternatively you can install all the packages separately using "pip install" in the pycharm terminal. If the package has been installed outside the virtual environment, pip will use cached files instead of downloading the package again.
Or you can install the packages using Settings -> Project -> Project Interpreter as the other answers suggests.
In PyCharm, go to File / Default Settings / Project Interpreter
On that page there is a plus in the bottom left, whatever modules you have installed through pip you may need to manually install there. Or you can change your project interpreter to make sure you are using the correct interpreter with all your installed modules.
Seems like the interpreter selected isn't where you think it is. If you're using a different interpreter than the system's default, it won't load the modules. To add the modules in PyCharm, go to your Settings, Project Interpreter, the Add Button, and install it with PyCharm's integrated package management.
If you did not "inherit global-site packages" upon creating the project, check in the project tree under External Libaries, if the folder is marked in red. I.e. if you do not use the systems default interpreter , Lib/site-packages could be marked in red.
To add those libraries afterwards, go to: File-Project(your project)\Project Structure,
select +Add Content Root, browse to site-packages (or the respective folder you want to add), and add it. After clicking OK the folder will not be marked red any longer. (tested in PyCharm 2020.3)

Python not showing existing venv in interpreter select

I have a shared flask web project I am working on with 2 other developers, one of the developers initialized the venv on his pc, uploaded his project structure to github from where I cloned his repo.
Now I when I start vscode and open the project folder, python does not auto detect the venv and asks if it should set is as the interpreter, the only option I have is the default system wide python install, and not the venv python interpreter.
I tried adding it to the list by using the command python:select interpreter and then finding the python.exe inside the venv/scripts folder, but this does not work and vscode still asks for a interpreter.
I also tried manually adding it inside of my workspace settings.json file like so
"python.pythonPath": "C:\\laragon\\www\\Proftaak\\venv\\Scripts\\python.exe"/
But vscode also gives an error on this saying the interpreter is not valid.
How would I fix this?
This is not expected to work as virtual environments are not designed or meant to be movable. They are meant to be created on each machine you need a virtual environment on. As such, I suspect that the virtual environment does not work outside of VS Code which could prevent it from selecting it as a possible working environment.

Can I setup project interpreter from .tox folder

The 'tox' interpreter is in the '.tox' folder, after executing command tox.
Are there any ways to set that interpreter to the PyCharm?
UPDATE: today this is made very easy with the PyVenvManage Plugin which provides a context menu entry to directly activate any virtualenv in .tox.
Yes that's possible. You open up Settings -> Project Interpreter -> Add local and choose the python binary you want to use. It does not matter if it lives in a .tox folder or somewhere else.
Also see pycharm help.

Unresolved references in PyCharm

I downloaded my old project from the repository and there are a lot of unresolved references now, how can I fix them?
If you are working with a virtualenv you have to configure your project to let Pycharm know what interpreter should be used and where are its dependencies.
https://www.jetbrains.com/help/pycharm/2016.1/adding-existing-virtual-environment.html
You should configure your Python interpreter and get Django installed. In the documentation you 'll see:
Selecting Python interpreter for a project
An interpreter can be made the project default when is it added.
To configure Python SDK for the current project follow these steps:
Open the Settings dialog box, and click Project Interpreter page.
In the Projects pane, choose the desired project.
For the selected project, choose SDK from the list of available Python interpreters and virtual environments. This list includes:
Python interpreters, which reside in standard locations.
Virtual environments, which reside under the project folder, or under the folder specified as an environment variable WORKON_HOME.
Other Python interpreters, installed locally or remotely.
If the desired interpreter is not in the list, click cogwheel button, and configure the desired interpreter as described in the
section Configuring Available Python Interpreters.
Apply changes.

PyCharm cannot find the packages in virtualenv

I have the latest PyCharm CE and am using it with virtualenv. I have defined the interpreter as the interpreter in the virtualenv. The Project Interpreter window in PyCharm lists all the packages I have installed. I confirmed this by running pip freeze > requirements.txt and running through the packages manually.
My problem is that PyCharm won't find certain includes in its editor windows, like Flask-Login:
In from flask.ext.login import current_user, login_user, logout_user, login_required the includes current_user, login_user, logout_user, login_required are all marked as unresolved references.
Am I missing something?
The problem may lay in PyCharm picking up faulty 'Interpreter Paths' for your virtual environment. Go here:
PyCharm (menu) -> Preferences (Menu option)
-> Project: <name> (Dropdown)
-> Project Interpreter (Menu option)
-> 'Settings' button (Looks like a gear)
-> More (Menu option)
-> Select your virtualenv interpreter
-> Click 'Show paths for interpreter' button (on bottom of list window)
Now that you're in this (admittedly tortuously found) location, you should see paths being used by this interpreter. If my theory is correct, these are pointing to global system locations. To add the virtual environment paths, you should click the + button and add corresponding paths that exist inside your virtual environment. Once you're done with this, it's a good idea to select the global system paths and click - to remove them. Click apply, and go to File -> Invalidate caches / Restart to reload PyCharm.
This should get your interpreter to be pointed to the correct location for the libraries you've installed into your virtualenv, and you should no longer be getting the import error. Note that even with this fix you will not see your libraries under the Project Interpreter, but they should be being loaded.
In the newest version of PyCharm (2016.1.4 in my case):
Settings
Project: name of project
project Interpreter
at the right side there will be a dropdown where you can choose the interpreter. There should be venv options.
See image below for better explanation (like they said, 1 picture worth thousands word)
I was also facing the same issue (includes are still not being found) even after Nathaniel Ford and dieend's correct suggestion. Make sure that your run/debug configuration as correct python interpreter selected:
I noticed that every time I open a different project it still has the venv from the project I was previously working on.
What I do is:
ctrl-alt-s (to go into preferences), then Project Interpreter/settings (gear icon), show all, then remove all the venv environments that aren't your current project (use the - sign). Restart, and you should be good to go.
Also note the accepted answer is no longer applicable to PyCharm menu structure. It is now File > Settings > Project > Project Interpreter > Gear Icon > Show All
The following steps detail the "nuclear" option:
Delete your project virtual environment directory (e.g. /venv)
Delete all other interpreters listed in menu option accessible by the route listed at the top of this post.
Close PyCharm
Delete the .idea directory in your project folder
Restart PyCharm, opening the project folder.
Go through the process of configuring a new interpreter.
That will pretty much get you starting from scratch.
Open up Preferences -> Project -> Project Interpreter, do you see the module there?
If yes, you might have another file somewhere in your project have the same name as flask.ext.login, this prevents pycharm from locating the actual module.
If no, you can click on the ... beside your interpreter and select more..., select your interpreter and at the bottom (beside the filter), click the Show paths for the selected interpreter, you can add the path of your module there.
For me, the easiest solution was to open the project in the root directory (my project has a server and client directories, thus the root directory contained both of them). When you open the project in the root directory, it is able to find the dependencies without messing with pycharm settings as it uses them by convention.
My two cents on this topic as I struggled myself with it recently.
Nathaniel Ford's answer is the good one except that this part:
-> Select your virtualenv interpreter
was unclear to me.
I tried several times with
~/.virtualenvs/python-audition-2.9/bin/python
whereas it only worked with
~/.virtualenvs/python-audition-2.9/local/bin/python
Notice the .../local/... in the latter path? It was really important in my case. And don't forget to File -> Invalidate caches / Restart to reload PyCharm.
Adding the lib directory in my virtual environment to sources in the PyCharm CE settings helped me.
My steps:
Preferences -> Project: -> Project Structure
Scrin1
Select the directory where you installed the libraries. In my case, this is "lib".
Mark the directory as "source".
Scrin2
After these actions, all my libraries were correctly imported into the py files I needed.
Easy solution: (PyCharm 2022.2.3) and Python 3.11.0 on Win11:
Create new venv Environment for the project, DO NOT inherit site-packages
Check, if jupyter-server uses new venv (Settings->Language&Frameworks->Jupyter->Jupyter Server)
Install all required packages (inbcluding jupyter!) to venv (e.g., requirements.txt and PyCharm)
Worked fine for me.
Goto /venv/bin/ and check all activate scripts. You venv path might be wrong.
I was not able to assign existing virtual environment to my project, but after going to
File -> Settings -> project interpreter-> show all-> click on '+'
to create a new virtual environment or we can choose the existing virtual environment, I am able to assign and use the existing virtual enviroments.

Categories