PyCharm Virtual Environment Setup [duplicate] - python

This question already has answers here:
How to activate conda environment from powershell?
(8 answers)
Closed last year.
I'm trying to get on with Python coding rather than fiddling about with settings getting in the way of development. Alas, it is not meant to be.
1 - I've installed Python 3.9 in Windows 10 from the Windows Store (running python in a powershell and instructions therein).
2 - I'm using PyCharm IDE.
I am very familiar with JetBrains products and building php projects. The transition to Python is not as straightforward. When I setup a new project and want to run code I need to set my configuration for the virtual environment.
I range from not being able to run any code through to perfect success with a lack of being able to import libraries in between.
In my toolkit is:
a) Edit Configuation - selecting a python interpreter. This never seems to make any difference no matter which one I choose. I only have options that are the right version but seemingly python.exe files in the project, in my Windows directory and all over the place.
b) Settings>PythonInterpreter>VirtualEnvironment>ExisitngLocation
When I get it to work it's usually the python.exe in the Windows directory that pip installs everything in. Selecting that interpreter then locates all the packages I have installed and I can import all the libraries I want.
I would like to (i) understand why I have so many options (don't I just use python.exe because I've installed it somewhere) to pick a multitude of python.exe everytime I build a new project. I have missed this aspect of Python.
I would like to (ii) understand how to get a project to reference the libraries I have installed on my machine such that I can access them properly in my project and spend less time trying to wire everything up and more time actually building the project out.

I had a similar issue initially.
My "solution" was to delete python from the Windows Store, install Anaconda and then follow these steps from a SO answer for enabling PowerShell to run python with conda
Details in creating projects with PyCharm can be found here (a webpage on Anaconda).
Then PyCharm just works... like a charm.

Related

PyCharm cannot install packages

Short description: two computers in the same network, in the new one only those python scripts work that use native packages.
I have Pycharm in my old computer and it has worked fine. Now I got a new computer, installed the most recent version of Python and Pycharm, then opened one of my old projects. Both the old and the new computer are in the same network and the project is on a shared folder. So I did the following:
File - Open - selected the project. Got a message that there is no interpreter
Add local interpreter - selected the latest Python 311 exe. So location of the venv is the same as in the old computer (because it's a network folder) but Base interpreter is pointing to the C drive of my new computer.
PyCharm creates a virtual environment and the code runs fine.
I select another project which uses imported packages such as pandas. Again, same steps as above, add local interpreter. Venv is created.
I go to File - Setting - Project and see that pip, setuptools and wheel are listed as Packages. If I double click one of these, I can re-install and get a note that installation is succesful, so nothing seems to be wrong in the connection (after all, both the old and the new computer are in the same network.
I click the plus sign to add a new one, search pandas. Installation fails. Same thing if I try e.g. numpy.
Error message has lots of retrying, then "could not find the version that satisfies the requirement pandas (from versions: none", "not matching distribution found for pandas" (pip etc. have the latest versions).
After few hours of googling for solutions, I have tried the following:
Complety uninstall and reinstall python and PyCharm. Checked that PATH was included in the installation.
Tried launching pip command from shell
Changed http proxy to auto-detect
Typed 'import pandas' in PyCharm, then used the dropdown in the yellow bulb but there is no install option
Started a new project in the new computer, tried to install pandas
All failed. I'm surprised that changing computers is this difficult. Please let me know if there are other options than staying in the old computer...
If you want to use venv in the network, please use SSH interpreter. Pycharm supports this method. Shared folders are not a recommended usage, For pycharm, it will consider this as a local file. If the file map is not downloaded locally, it will make an error.
Another way is to reinstall the project environment on the new computer through requirement.txt. Reasonable use of requirements.txt can effectively avoid many project bugs caused by environment migration or different dependent versions. Before installing some scientific module such as pandas, it is recommended to install visual studio build tools, such as gcc ...
This took a while but here is what happened. Package installation did not work in project settings. Neither did it work when you select Python Packages tab at the bottom of the screen. The only thing that worked was to select the Terminal tab and manually install (pip install) there. We use a trusted repository but for other users, the easier package installation methods work. Not sure why they do not for me but at least there is this manual workaround.

General question about python interpreters (having multiple interpreters, pyenv, etc)

I'm new to coding and I was wondering if someone could explain what exactly python interpreters/environments do and the how that relates to the python versions and packages that can be used in certain projects.
I was trying to code a twitter bot with python and I use VS code as my IDE. While trying to import tweepy into my python code, I noticed I kept getting an error that it couldn't import. After some googling I realized it was because I had to set the python interpreter of the python file to the one that had tweepy installed to. So I changed it and the error with tweepy was solved. But I noticed that I have a bunch of python interpreters in seemingly different locations (screenshot of the interpreter options I'm given). The interpreter that fixed the issue was the one in the pyenv path.
I had installed pyenv some months back because I wanted my terminal (I'm on macOS) to automatically launch python3 when I typed python into the terminal. However, I don't really know what it does beyond that.
So my questions are:
Why do I have multiple python interpreters? Is there a way to get rid of the ones and just keep the one from pyenv (like clean up the ones in /usr/local/bin/python3, /opt/homebrew/bin/python or /usr/bin/python3 since I'm not using them)? or should I not do that?
What exactly does pyenv do? Is it okay that my python libraries are getting installed to pyenv by default? Or should I change it so that it's getting installed to homebrew or one of the usr/bin paths?
Sorry, this is my first time asking a question here so I might not sound that cohesive.
The interpreter is the program which executes a python source file. This is a program like any other, and you can have various versions of it at once (indeed python is quite good at being self contained, and multiple versions will live alongside one another quite happily).
Macs ship with an outdated python 2, used for something in the system. If you remove or update it, things may break. So you install your own python 3 somewhere. If you want to load the new python when you type python in a shell, that shell's $PATH needs to point to the right python. You generally set this in something like ~/.profile.
Pyenv is a tool for managing multiple python interpreters. It's usually used on a per-project basis, to test code against multiple pythons. Further to complicate things, it's often used alongside a virtual environment tool like pipenv. Using it to avoid manually fiddling with $PATH is fine, but a slightly orthogonal use case. All pyenv does is to put little scripts with the same name as python executables somewhere in your $PATH where they supercede anything else. These scripts then figure out which python should get called in this case. So pyenv isn't installing anything, it's just working out which python is going to do it.
The solution to all your problems turns out to be quite simple:
python -m pip install abc
Get into the habit of installing stuff with the python you want to use already set up in the shell, and calling pip like that. That way whatever you use it will always be installed in the right env.

Integrating PyCharm with Abaqus

I'm using PyCharm Community V2021.1.3 and I would like to integrate Abaqus CAE (2021) with PyCharm so that I can write/debug/run scripts for Abaqus.
Currently I write code in PyCharm/Sublime or some other editor and then run the script through either the command line function or within CAE. The real issue that I have is debugging the script. Abaqus comes with it's own IDE (Abaqus PDE) but lets be honest.... there's A LOT of room for improvement and it absolutely sucks for debugging. In the past I have ended up copying/pasting sections of code directly into CAE kernal commands just to try and pinpoint where issues might be. Far from ideal.
My PyCharm installation is using a Python 3.5 interpreter. I try to keep my projects segregated as I often have to switch between various packages and versions depending on what I'm working on. I would ideally like to create a new virtual environment that hosts the Abaqus version of Python (2.7.3) and packages associated with that version of Python. Generally I use Conda to create/maintain virtual environments however, in this case, Abaqus has installed the relevant version of Python and several packages as part of the Abaqus installation procedure. Is it necessary for me to create a new virtual environment if the Python version and packages I need are already installed locally? If I point the interpreter directly to the Abaqus installed Python version and then screw up later is that going to be a major fix? Normally if something goes wrong in a virtual environment (mixed up package requirements or that) I just delete the environment and set up a fresh one...
I'm fairly new to PyCharm (<6 months) and so I'm unsure of how best to integrate these two pieces of software (PyCharm and Abaqus) without completely messing up my Abaqus installation. This similar question: How integrate Abaqus python libraries into a project hosted in PyCharm suggests adding a new system path (PYTHONPATH) to system variables. Restarting PyCharm and then pointing the Pycharm interpreter to the system interpreter. That kind of makes sense to me but I'm not sure whether that will effect my existing virtual environments for Python 3.5? The whole point of having virtual environments is that you don't run into issues with package compatibility so I think it should be OK? If I set up a new virtual environment for Python 2.7.3 (same as the abaqus installation) and get a range of packages associated with that python version then I'll still be lacking some of the Abaqus specific packages required for checking types etc. That's why the previous post has probably recommended pointing the interpreter directly to the Abaqus installation. Is there a way to clone that version of Python and packages to a new conda virtual? Any help, tips, relevant posts etc. would be greatly appreciated.
It is impossible for us to debug the abaqus/python script since we don't have the access to the abaqus/python source code. But there is an alternative way to make things better, if type hints of the abaqus/python functions are provided, it is much more easier to debug the script. Fortunately, abaqus provides the documentation for almost all of the python functions in its website, based in the online documentation, I restructured the abaqus/python classes to provide type hints for the abaqus/python functions.
According to this consideration, I have developed a package named "pyabaqus" to provide type hints for abaqus/python scripting and submit the script file to abaqus kernel, you can check it in pyabaqus or github.com/Haiiliin/pyabaqus, hopes it helps you.

Jupyter Notebook cannot find certain modules

As part of a web scraping class I've been instructed to download a few modules from the command terminal (requests, bs4, lxml). These have seemingly been successfully downloaded to my computer, yet my Jupyter Notebook AND PyCharm virtual environment cannot find them.
I am a beginner so I'm sure there are some big things I'm missing, any help would be appreciated.
How did you install Jupyter?
Wich package manager are you using? (eg. what did you type to install the packages?)
Wich modules can it find and which not?
Usually you want to make sure your modules are available in the environment you are using. ( Enviornments are like "versions" of python. You could run multiple versions like 3.7 and 3.9 on your Computer but also also different builds from one python version.)
Pycharm builds its own environment. Make sure you use the enivorment you installed your packages to in Pycharm. Pycharm allows you to modify and create new environments very easily
For PyCharm it might be useful to check out their help page.
I figure this could help: https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#add_new_project_interpreter
The interpreter is basicly the a customized version of python. Pycharm keeps track of the modules you need for every project. So you want to create/select an interpreter which has the packages you have installed.
hope it helps
cheers

I must install django for every single project i make?

i am new to Python programming language and Django. I am learning about web development with Django, however, each time I create a new project in PyCharm, it doesn´t recognize django module, so i have to install it again. Is this normal? Because i´ve installed django like 5 times. It doesn´t seem correct to me, there must be a way to install Django once and for all and not have the necessity of using 'pip install django' for each new project I create, I am sure there must be a way but I totally ignore it, I think I have to add django to path but I really don´t know how (just guessing). I will be thankful if anyone can help me :)
pycharm runs in a venv. A venv is an isolated duplicate (sort of) of python (interpreter) and other scripts. To use your main interpreter, change your interpreter location. The three folders (where your projects is, along with your other files) are just that. I think there is an option to inherit packages. I like to create a file called requirements.txt and put all my modules there. Comment for further help.
In conclusion, this is normal.
There are 2 ways of starting your project in PyCharm.
To use a virtual environment where you have to install the external libraries into this environment.
To start using the existing interpreter. (If you chose this option you don't have to install it again installing it only once globally would be enough)
So when you start to create a new project choose the existing interpreter.
Doing this will not require you to install it every time(But is not recommended for big projects as there would dependencies on different versions of libraries).

Categories