VS Code pytest can't find any tests in workspace - python

I am trying to migrate my python project from pycharm to vs code. I have PyTest setup in vs-code but none of the tests are being found even though running pytest from the command line works fine. On the left side in the "Testing" section where should be project tests I am getting the error: "ModuleNotFoundError: No module named 'YADAYADA'", maybe somebody knows how to fix this?
I have tried to add init.py file to every folder and subfolder

Vscode uses the currently open folder as the workspace. You need to open your project folder with vscode. In addition, you also need to install necessary extensions for it, such as Python.
Tests can be configured in the test panel on the left. You can also use Python: Configure Tests from the command palette at any time.
Read the documentation for details.

Related

Question regarding package management in PyCharm IDE

I have been using PyCharm since I began learning Python because of its amazing UI that helped me learn a great deal about the language. As I progress into more advanced projects, I am beginning to prefer using a text editor / command line combo so that I can build my own venv's and have better access to source control. My question is, how does PyCharm manage custom local packages that I created so that I can import them wherever I want in the directory? For instance a project that I have built exclusively in PyCharm that runs will raise numerous import errors when trying to run that same project in VS code, or even a command line shell (yes I did have the PyCharm created venv activated before running on both attempts). For further examples, here is the project structure I am confused about:
RootDirectory
package_1_folder
__init__.py
pckg_1_class.py
program_using_pckg_1_folder
class_using_pckg1class.py
venv
The above structure has no issues being imported and used in PyCharm, however VS code / Sublime when used with command prompt / gitbash will raise either an ImportError or a ModuleNotFound error. I have even gone as far as adding the desired packages to my laptops windows PATH, using sys.path.append (I know this is not good practice I was only trying to get it to work), and even modified the .pylintrc file with the project path with no success. Any help explaining why these errors are happening would be greatly appreciated :)
NOTE:
I have been able to use the packages in VS code as long as the program importing the module is located at the root directory level, but not in its own folder in the root directory. Again, this statement WILL work in PyCharm, I just want to know how PyCharm is able to achieve this.
After numerous attempts to locate how the system was keeping tack of module within the IDE, I found that my answer was not visible from the IDE. I found a .idea folder in my root directory that contains a few .xml documents that manage the directory including where to read modules from.

Import Error: Python Unit Tests, on Jenkins

I have a few unit tests, which I wrote in Python 2.7. (On PyCharm)
All of these test run locally just fine. However, I am trying to run them on Jenkins automation server.
The problem is that when running on Jenkins I get: "ImportError: Failed to import test module: test_one." I believe this is because an import within that test, which is located a few levels above in the hierarchy. (See hierarchy below)
modules
jira
jira_module #File to be imported
unit_tests
test_one
So, test_one contains the line "import modules.jira.jira_module as jm"
But the command line on Jenkins apparently doesn't like the path using dots. Does anyone know how to solve this issue? I've attempted using runpy - run_path, but that did not work either.
For this line to work
import modules.jira.jira_module as jm
module package modules must be located within PYTHONPATH.
The right approach is that the root directory (which contains modules and unit_tests folders) would the one in which you start the tests. Usually in the project root a bootstrap file is created to run the tests (runtests.py or runtests.sh or similar).
If you don't know how to this, or find this difficult, you could tweak the paths adding the root folder to sys.path in test_one.py. But this is ugly and not right.

Test Works in PyCharm Community and Fails in Professional

I switched to PyCharm Professional in hopes of using the code coverage tool, but it seems to have broken my test configuration. I have a project with a project folder, config folder and test folder like so:
\project
\my_python_app
\test
\config\config.yml
In Pycharm Community, I was able to set the test path to the \test folder and the working directory to \project, which allowed me to access the config file. But when I switched to pro, it broke and said
ImportError: Start directory is not importable: '/home/project/test'
I need the working directory set to '\project' in order to access the config file. Any ideas on why this would be happening or suggested workarounds?
Pycharms importing behaviour can be quite annoying, I had similar problems in the past. I could resolve then using the "Source Folder" setting in the project structure tabs. E.g. set the whole project folder (and maybe test) as "Source".

IntelliJ Python unittests cannot import modules

I'm trying to configure my IntelliJ Ultimate 2017 IDE to run Python unit tests. It's not working.
The project is a Gradle project, and most modules are Java/Scala. There are several Python modules though.
The Python plugin is installed and the Python facet added to the appropriate modules.
The Python Interpreter is set up fine. The IDE can actually move around in the code with a ctrl-click action.
Under 'Project Structure' -> '<mymodule>' -> dependencies both the interpreter and the <Module source> are included.
Each Python module has a 'content root'. Immediately under this is a src folder, under which there is a test folder. The contents of the test folder mirror the src folder, except that files are prepended with test_ to avoid Python name collisions.
The test configuration has both 'Add source/content roots to PYTHONPATH' options checked. The interpreter is specified.
If test is marked as a tests folder within the module settings, I can right click and 'run tests' for the tests in that folder. However, ImportErrors occur, because it can't find modules within the src directory.
There are __init__.py files in all the appropriate places.
IntelliJ gives you what it is running, and if I run that on the command, it works, something like:
python -m unittest discover -s /Users/nathanielford/myrepo/mymodule/src/tests -p test_util.py
It appears as though the root path that is being used is set to wherever the unittest file I'm running is: it doesn't use src as the path, and so it cannot find things to import. There is no obvious place to specify this behavior. It didn't seem to work like this in the past.
This started happening recently, after an IntelliJ update. It's possible some configuration became screwy and needs to be fixed, but I don't know what that is. It's also possible that some bug was introduced into IntelliJ, but I don't know how to identify that, either. What am I missing?
Had the same issue and resolved it by no marking the test folder as excluded but just moving it out of the source.
So if the structure is:
src
--->package
--->resources
test
mark in IntelliJ src folder as Sources and src/resources as Resources.
You tests will run from within IDEA.
As I am new to python, I am not sure whether this will affect the packaging. Doing a build though doesn't export the test folder.
Hope that helps.
I have similar problems: cannot import manually installed module.
I try to add classpath in Mac environment parameters and intellij SDKs, but it doesn't work.
My final solution is to add the classpath of the module to Run/Debug Configurations:
Open Run/Debug Configurations and select your unittest class:
Run --> Edit Configurations...
Add your module's classpath to Environment variables:

Python peek definition works in vscode, but pylint complains about being unable to import

I want to test some python files in a project before installing them. For this I entered the folder to be installed in my vscode workspace settings as follows:
"python.autoComplete.extraPaths": [
"${workspaceRoot}/lib_python/release"
]
Now I have a test file in my workspace that imports from the above mentioned folder. Suggestions work fine, it also gives me definitions while hovering with the mouse over a class name from the imported file. However, half my code is underlined in red, because pylint says it does not know the definitions and cannot import.
Question: Is there a way to also make whatever part of the python plugin by Don Jayamanne is responsible for the red underlining find the additional import folders?
So what worked for me now is to set additional paths in a .env file in the project's root folder. This file will be read by the python extension of vscode.
Example:
PYTHONPATH=./lib_python/release
In User Settings -> Extensions be sure python command is the right one. On my Mac, python refers to Python 2.x, however python3 refers to Python 3.X. I simply changed VSCode's python command to python3 and this resolved my issue.

Categories