![python runner get current directory python runner get current directory](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/original_images/Jp2ssv5LiOSjkCvT5Cmc5njBBbUCy3DNzqcaUE65Sws4ywWa4wOX0ZD5E9iQrWf9uOAMOYBxNl.png)
Now modify the import statements in your test_*.py files. Navigate into the project folder my_project.
PYTHON RUNNER GET CURRENT DIRECTORY INSTALL
Install the package in Developement Mode. Create my_project/setup.py with that content:.Example configurationĪs an addition to my first Create the project folder and modify the folders inside it as I pointed out above.Ĭreate a my_project/setup.cfg with that content: Just import it because the package is known to the system.
PYTHON RUNNER GET CURRENT DIRECTORY HOW TO
Now your unittest never need to care about where the package is installed and how to import it. In that case not a real package is build and installed but only symlinks are used to expose your package folder (the source in developer version) to the operating system as it would be a real release package. How could this be done? You have to "install" your package in Developement Mode. Install in Development Mode via -editable Your goal should be just to do: from my_package import my_module I assume that your test_work.py file looks like this: from. Now you wonder how to import your package in your test_*.py files, right? my_project/īut read further to see how I would improve this with the so called src-layout. Look on the dates of blog-posts, tutorials and docs! Good start is setuptools documentation.Īlways separate your package folder (where the _init_.py file is in) and the tests folder.īecause of that in your case you will need an extra project folder like this. And keep in mind that when you look around in the internet you always find outdated (and today wrong) information's about packaging python projects. You should modify your folder layout to the current recommended (or see pytest docu). Or is it not worth it and I have to test from the parent directory due to the current structure? How can I run python -m unittest or nosetests or pytest from within the package and have them run as a _package_ with the correct hierachy? As this will test all modules found in the directory above. This does not feel right, I want to be able to test and have my continuous integration test without having to cd. With the following output when printing in test_work.py: print(_package_) # my_package.tests isfile ( ) return True if the path points to a regular file, False if it points to another kind of file. Then start a loop and get all files using isfile ( ) method. so I am no longer in my_package and run python -m unittest the tests run correctly. print(item.name) First of all call iterdir ( ) method to get all the files and directories from the specified path. I also printed in test_work.py: print(_package_) # tests
![python runner get current directory python runner get current directory](https://pythonguides.com/wp-content/uploads/2021/03/Check-the-current-working-directory-1-300x177.png)
This is because in test_work.py, it has a relative import: from. This results in: ValueError: attempted relative import beyond top-level package I would like to cd my_package and run python -m unittest. There are also many relative imports used in the modules and tests folder. I have a python package with a tests folder within it.