How to refactor files/python modules in VScode? - python

I am able to refactor variables in VScode, but I would like to refactor the module names so that the import of those modules are automatically resolved.
A similar feature is present in JetBrains Pycharm but I could not find in VScode.

Such a feature doesn't exist in the Python extension for VS Code. To request it, open an issue at https://github.com/microsoft/vscode-python .

Update: This issue has been raised in issue 4628. The feature still doesn't exist for now, but hopefully it will soon.

Related

What Does PyCharm Do To Make It Easy To Import Other Modules?

I have a question regarding simple imports that I cannot get my head around.
Take a look at the attached screenshot to see my project layout.
The file somefile.py imports the class SayHello from a file called someclass.py and calls it. someotherfile.py does the exact same thing. They both use from someclass import SayHello.
In Pycharm both files run. However, From the command line or from VSCode somefile.py runs, but someotherfile.py errors out with the following error:
ModuleNotFound: No module named 'someclass'.
I believe it has something to do with PYTHONPATH/environment variables or something like that, but every explanation I have read has confused me thus far (Even this one which I thought was going to set me strait Relative imports for the billionth time).
Can someone explain in simple terms what is happening here? What is Pycharm doing by default that other editors are not such that my imported modules are found? How can I make someotherfile.py work in VSCode without modifying it?
Thanks in advance!
Pycharm adds your project directory into python paths by default. See configuration of "Pycharm run" that you execute and you shall see few checkboxes like
If those checked Pycharm creates PYTHONPATH environment variable for you that instructs Python where to look for someclass module.
You will have to configure VSCode to define PYTHONPATH environemnt variable for python command you run and include your root project directory path on it.
TLDR: Mess with the line starting with ./automated pointing it to various directories in your project until it works haha.
Long rambling answer: Alright now that I am not in a frenzy from trying to figure this out and it has been a day, I feel like I can make a conherint response (lets see if that is true).
So my original answer was an attempt to simplify my problem into what I thought was the issue due to a ModuleNotFound error I was getting. I had been trying to get unittests in Python inside of Visual Studio code to work (hint hint just use Pycharm it just works out of the box in there), but the integrated testing feature or whatever could not find my tests giving ModuleNotFound as the reason.
The solution to my problem actually just concerned the line ./automated-software-testsing-with-python/blog.
In the below screenshot the relevant part is ./automated-software-testing-with-python/blog.
This is it when it is correctly configured (My tests work Woo hoo!)
Now you can go ahead and read the official documentation for what this line is doing, but I am convinced that the documentation is wrong. Due to how my code is structured and how my files are named, what the documentation says its looking for definitely does not exist. But that is another can of worms...
I actually got it to work by accident. Originally when you go though the wizard to set up what's in that screenshot above, it defaulted to ./automated-software-testing-with-python which did not work. I then manually edited it to something else that was not the below screenshot hoping to get it to work.
It was only when I pointed it to that blog directory on accident (thinking I was in a different file due to trying to debug this for hours and hours in a blind rage) that it ended up working.
I did a bunch of PYTHONPATH manipulation and Environment Variable mumbo jumbo, and I originally thought that that had an effect, but when I cloned my repot to my other machine (which did not have any of that Environment Variable PYTHONPATH stuff going on) it worked too (again, provided that the line in question pointed to blog.
Ok hopefully that might help somone someday, so I'll end it there. Not to end on a bitter sounding zinger, but I will never cease be amazed by how doing such simple things such as configuring the most basic unit test can be so difficult in our profession haha. Well now I can start working. Thanks for the help to those who answered!

option to "import package" in Pycharm no longer available

Before, when I would instantiate a class that I haven't imported, I would be able to hover over the class (or whatever was missing), and see an option to import it like the screenshot
Where it says "Create class" I would have the option to import the package. Since I updated my pycharm to 2021.3.1, that option seems to be gone for external packages. It still seems to work for internal packages, say, json. This is vital to my productivity. Has anyone else run into this?
I ended up deleting and recreating my .idea directory in pycharm. Once I did this, the problem was resolved.

Unable to resolve 'requests'. IntelliSense may be missing for this module. Visual Studio/Python

Using Visual Studio with Python. I'm getting this warning when importing requests.
Unable to resolve 'requests'. IntelliSense may be missing for this module.
In the Python Environments tab, you can see that I have requests installed down in my packages.
I'd like to have IntelliSense while working with 'requests'. Is there something I may have overlooked?
1.Maybe it's something similar to this issue. Deleting the init.py in top-level of your project can help resolve this issue.
2.If the above not helps, please try reloading the project.
After my test, sometimes in a python application, after the first time we install a python package, the intellisense for the new package in current project won't work until we reload the project file to recognize the new package.
For this situation, the easy workaround is: Right-click the project in Solution Explorer=>choose unload the project=>Right-click it again and choose reload the project. Then the intellisense for the new package would work well.
Hope it helps and sorry for the delay!

Inconsistent Import Error in Python

I just have a quick question about an error I've been getting when I try to import:
from psychopy import gui
So if I try to import this code in one program I have no problems, however, if I try to import it in another I get:
"ImportError: cannot import name gui"
Does anyone know why this might be happening? Why does it work for one problem, but not the other? Also, I feel like it wasn't doing this before, and it just started suddenly. Any advice would be greatly appreciated.
UPDATE: I think Jon's answer is the correct one. If I was right, you should get an error "no module named psychopy".
Given that you tagged this question with the psychopy tag, my guess is that it works if you run it from the psychopy app and that it doesn't work if you run it from another editor or command line.
The reason is that psychopy is currently shipped as a bundle that comes with it's own python and a lot of modules/dependencies, including psychopy. The system is not made aware of these modules via the PYTHONPATH.
You can make them available system-wide by either (1) following the steps outlined here or (2) use the conda based installation described in this post in the psychopy-dev list. The latter feature is still work in progress but will probably eventually mature to be the default install option.
I think the other answers are wrong ;-)
I think if you had a different virtual environment or installation then the error in your code would indicate "No module named psychopy"
The fact that it finds something called psychopy but no sub-module called gui is a different problem. Often this occurs if you have a folder or file called psychopy next to you current working directory (eg. next to where you launch the script). Then Python thinks that's the psychopy module but can't find gui within it.
So, do you have a folder called psychopy? Rename it psychopyStuff.
I think you are using different virtual environments for both the projects, and so the package is installed in one virtualenv, and not in the other.
To verify this is the case, do a pip freeze in both the projects and compare the results.
If there is a single environment, the output will be same, otherwise the outputs will be different amongst the two.

Intellij python no recoginiize lib

I use intellij with python plugin.
when I want to import python libs like
import random
I got editor error.
No module named random less... (Ctrl+F1)
This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.
when I run the code every thing is ok
what can I do to make the intelij recognize this libs?
You may have fixed this by now, but I was having the same problem and finally solved it, so I figured I'd post the solution for anyone who came here by Googling/Binging the error message:
I went to File > Project Structure > Modules, highlighted the main module, then pressed the plus sign and then "Python" under "Framework".
Hope that helps you or someone else.
may be my intellij version is different with you guys.
on Windows platform I fix this problem by:
1.File > Project Structure > Modules
2.on the module's dependencies panel,change the module SDK from JDK to python
3.done

Categories

Resources