Auto loading packages to python project in pycharm [duplicate] - python

This question already has answers here:
How do I use installed packages in PyCharm?
(14 answers)
Closed 1 year ago.
I am completely new to pycharm so this might be obvious. But currently every-time I load a project in pycharm I need to re-install the packages. Is there a way to automatically load them, or keep them on the project?

where you are installing the packages, to the python system interpreter or to the virtual environment ? If you are creating virtual env for all the projects you need to install packages everytime but if you use system python interpreter one time installation will be enough no need to install the same package again.

The default option of PyCharm is to generate a fresh virtual environment.
Which is safe, but you need to re-install packages.
See below how to select an existing environment instead.
So you could set up one environment, install what you usually need and always use that. This is fast, but comes with a big downside: Later updates to this or other installations may break programs you wrote earlier.
As you can see, there's a checkbox that says: Make this environment available to all projects which will help you doing this.

Related

What is the differene between "Python 3.9" and "Python 3.9 (venv)" interpreters? [duplicate]

This question already has answers here:
What is a virtualenv, and why should I use one?
(4 answers)
Closed 2 years ago.
If I start a new python project, pycharm sets the Python 3.9 (venv) as default interpreter. With this, I need always reinstall packages, that were already installed, so I changed to Python 3.9 interpreter, but I don't know what is the real difference, and wich one should I use.
The difference is that, unlike the base interpreter, which is not context dependent, the venv (virtualenv) interpreter serves a specific project.
The advantage of using a virtual environment is that you can maintain different packages and package versions depending on the requirements and dependencies of each project separately.
In order to avoid reinstalling the same packages each time you can:
Inherit global packages. By default the venv interpreter comes with the minimal packages installed, but it is possible to inherit the global packages.
I see you are using PyCharm, so you can follow these instructions under "If New environment is selected:", section 3.
You can also achieve this using the virtualenv package directly by adding "--system-site-packages" to the creation command (e.g.
virtualenv venv --system-site-packages).
Create a requirements.txt file. Pip can install packages using a file the specifies the packages (and optionally package versions) you would like to install. You can run:
pip install -r /path/to/requirements.txt
Where the file is usually located at the root folder of the project.
In order to create such a file from an existing interpreter you can use the command
pip freeze > /path/to/requirements.txt
I personally prefer the second option.

Questions Virtualenv [duplicate]

This question already has answers here:
How can I set up a virtual environment for Python in Visual Studio Code?
(23 answers)
Closed 2 years ago.
just wanted to set up my new MacBookPro M1. As I want to organize my MB this time, i want to start using virtualenv.
So, what I've done so far:
installed brew
installed virtualenv
set up a dir, in there create my first env called sec_env
installed some packages for testing
Now I want to use my virtualenv:
I started it, source sec_env/dir/activate
AND now here we go, I want to code something in this env. So I start up my code-insiders and try to import the package i already installed....does not work ;( (EDIT1: Maybe i failed config it inside vs code?)
Do I missunderstand the use of virtualenv? I thought of it kinda like a virtual machine...So i can install package in need for one project and code it. But if i work on another, i would just switch, start up my vs-code again and keep writing on the other project.
Or is the problem just, that all the project I want to code have to be inside the dir of the virtualenv(sec_env)? At the moment , I have a dir virtualenvs where I store all my environments , start one up and change to desktop to work . And all the projects are on my desktop.
Would be awesome if someone give me any tipps on this, or another way to separate my different projects. I am super new to this topic, since I used different virtual-box images before...now i am forced to use something else...M1 :D !
Your understanding is generally correct as virtualenv are a way to keep projects' dependencies separated from each other, like a VM would.
Your code doesn't need to be in the same directory as where your virtual environment, but many people tend to organize it that way for the sake of convenience. That way you don't need to think about what venv you coded a project with since it's right there in the directory.
With your steps, I think you installed a package before activating the environment. Doing it in that order installs the package in your system site-packages, not your virtual environment packages. Before you install a package, you need to activate your environment. Also, it appears from How to tell Homebrew to install inside virtualenv? that homebrew doesn't support installing a package into a virtual env. So in order to install packages into a virtualenv, I would suggest using pip as your package manager.
So the sequence of commands would be...
source <path to virtualenv>/dir/activate
pip install <modules you want to install>
# Now you can run your code that references those installed modules.

How can i choose a different python version after set a virtual enviroment ? im on win10 [duplicate]

This question already has answers here:
Use different Python version with virtualenv
(41 answers)
Closed 2 years ago.
i have 2 version of python installed on my win10 machine 3.6 and 38
depending on my project
how to launch one rather than the other ?
Do i need to choose before i set the vrtual enviroment ?
basically i was wondering after setting my virtual enviroment, how to choose the one i want to use for the project ?
any indication?
id love to do like i do in choosing django version.. for example pip install django==2.1 , how can i do for choosing python version ?
(This is assuming you have installed virtualenv package)
(Applies to windows only)
Ideally, you can switch between different virtual environments in python using the following command.
.\env\Scripts\activate
You can check if it points to right python instance using
where python
Once you are inside a venv, pip automatically installs packages under that particular version of python (python instance of the venv). So you shouldn't have any issues there.

Anaconda overriding python as the default site-packages directory windows 7

from what I remember of middle school, anacondas and pythons are large snakes that overpower their prey, but anacondas are much bigger and heavier, which may be how it is overtaking my python pathway:
I installed psycopg2 with an easy .exe on windows 7 last night, and it worked (to the anaconda path)- but when I tried to start a Django app, it is looking in Python27\lib\site-packages. I allowed Anaconda to add itself to the pathway when I installed it because a post recommending it said to, and I also have Python set up correctly with a windows path variable.
This makes me wonder, should I actually uninstall python2.7.10, since Anaconda came with python 2.7.9 built in, and use the anaconda prompt for everything, and also delete python 2.7 from my path variable in windows? So,exactly
What is the best way to use Anaconda with Django?
I currently spent the weekend trying to tackle a similar issue. I have Anaconda (Miniconda3) installed for analytical work, however I also wanted to work on a Django project. In the end I ended up using an Anaconda virtual environment to work on my project. Here is basically what i did in cmd:
>mkdir mysite
>cd mysite
>conda create -n mysite-env python=3
>activate mysite-env
>conda install django
Basically this created a virtualenv in the envs folder in my Anaconda installation and I can create/work with django without having to worry about multiple python installations. Not sure if this is the best way, but its the way that works for me.
The issue you are having is that you may not have looked into what Anaconda is used for or what it solves. This step was pointed out in the installation steps when it adds Anaconda to your PATH. Being that Anaconda installs Python and has "environments" there is no reason to have 2 versions of the Python installation on your system, but Anaconda allows you to have different versions of Python with environments. Only one can use the "Python" command unless you execute it from the python.exe directory or make a separate alias as the python command and point it to the Anaconda or other Python installation. Again this is pointless as you can create a new environment with your Django project and requirements.
In short, having 2 separate Python installations is pretty much taking away what Anaconda helps solve.
In Windows 10, open Anaconda Navigator, search Not installed for 'django'. Click the check box, then hit Apply.

Using (Ana)conda within PyCharm

I've got Pycharm 4 running on my Linux (Ubuntu 14.04) machine. In addition to the system python, I've also got Anaconda installed. Getting the two to play nicely together seems to be a bit of a problem... PyCharm provides some interesting integration for virtualenvs and pip, but the Anaconda Python distribution seems to prefer using its own conda tool for both activities.
Is there a relatively simple/painless way to be able to use conda in conjunction with PyCharm? Not just as an alternative interpreter i.e. point PyCharm at the Anaconda Python binary for a project interpreter, but to be able to create, source/activate and deactivate virtual envs, add/remove packages in those virtual envs, etc.
Or am I going to have to choose between using Anaconda (and having a more recent and up-to-date python than may come with the system), and being able to use PyCharm's features to their fullest extent?
I know it's late, but I thought it would be nice to clarify things: PyCharm and Conda and pip work well together.
The short answer
Just manage Conda from the command line. PyCharm will automatically notice changes once they happen, just like it does with pip.
The long answer
Create a new Conda environment:
conda create --name foo pandas bokeh
This environment lives under conda_root/envs/foo. Your python interpreter is conda_root/envs/foo/bin/pythonX.X and your all your site-packages are in conda_root/envs/foo/lib/pythonX.X/site-packages. This is same directory structure as in a pip virtual environement. PyCharm sees no difference.
Now to activate your new environment from PyCharm go to file > settings > project > interpreter, select Add local in the project interpreter field (the little gear wheel) and hunt down your python interpreter. Congratulations! You now have a Conda environment with pandas and bokeh!
Now install more packages:
conda install scikit-learn
OK... go back to your interpreter in settings. Magically, PyCharm now sees scikit-learn!
And the reverse is also true, i.e. when you pip install another package in PyCharm, Conda will automatically notice. Say you've installed requests. Now list the Conda packages in your current environment:
conda list
The list now includes requests and Conda has correctly detected (3rd column) that it was installed with pip.
Conclusion
This is definitely good news for people like myself who are trying to get away from the pip/virtualenv installation problems when packages are not pure python.
NB: I run PyCharm pro edition 4.5.3 on Linux. For Windows users, replace in command line with in the GUI (and forward slashes with backslashes). There's no reason it shouldn't work for you too.
EDIT: PyCharm5 is out with Conda support! In the community edition too.
Change the project interpreter to ~/anaconda2/python/bin by going to File -> Settings -> Project -> Project Interpreter. Also update the run configuration to use the project default Python interpreter via Run -> Edit Configurations. This makes PyCharm use Anaconda instead of the default Python interpreter under usr/bin/python27.
as per #cyberbikepunk answer pycharm supports Anaconda since pycharm5!
Have a look how easy is to add an environment:
Continuum Analytics now provides instructions on how to setup Anaconda with various IDEs including Pycharm here. However, with Pycharm 5.0.1 running on Unbuntu 15.10 Project Interpreter settings were found via the File | Settings and then under the Project branch of the treeview on the Settings dialog.
this might be repetitive.
I was trying to use pycharm to run flask - had anaconda 3, pycharm 2019.1.1 and windows 10. Created a new conda environment - it threw errors.
Followed these steps -
Used the cmd to install python and flask after creating environment as suggested above.
Followed this answer.
As suggested above, went to Run -> Edit Configurations and changed the environment there as well as in (2).
Obviously kept the correct python interpreter (the one in the environment) everywhere.

Categories

Resources