Ex46 - can't import nose

When I’m writing up code in setup.py, nose module simply does not work.

I did some snooping around, and figured out, that nose is installed in (lpthw) virtual enviroment…

… but VS Code does not recognize it, and thusly, does not import it.

How can I fix this?

I’m no familiar with Pylance but you should have a way of either installing nose right there in your VSCode setup, or telling it where your virtualenv is for that project. Have a look around the settings to see what you can do.

1 Like

Figured it out.

While editing setup.py and NAME_tests.py, i couldn’t simply open them in VS Code from level of Windows, I had to use

(lpthw) > code setup.py

command, so VS Code launches in restricted mode, from level of (lpthw) venv.

Unfortunately, nose still malfunctioned, so I used Pytest and everything ran correctly.

Thank you for response!

Nice, yep pytest seems to be the solution to nose.

1 Like

I have another quick question, but don’t want to make another thread.

I have completed all of ex46 quiz. Made some rough script in NAME folder, made a little module in bin folder, edited setup.py accordingly and managed to install it and then uninstall it with pip.

But before I move on to ex47, I wanted to test that installed module, out of curiosity.

I made another directory for 3rd project. Created a python script, where I imported my newly installed module from quiz, but when I run it in Powershell, it does not find that module (it’s present in “(lpthw) > pip list” though.

How do I fix this? And should I even bother with this at my current level, or should I move to ex47?

Hmm, that’s a tough one. You should look at your setup.py and make sure everything is filled in, but otherwise it’d take forever to debug this off the forum. Look at what it says, and also confirm that your new shell is using the original virtual env. It might be that you did this:

  1. start virtualenv
  2. make project
  3. install project in virtualenv form #1
  4. start new OS shell
  5. pip list … BUT you’re not in virtualenv from #1 so it won’t list that

Try that.

1 Like

Can you delete or edit this and do a new one with pastes instead of images? I really can’t read the text in these images. You can just grab the text, copy it with the Edit menu (or hit ENTER to copy it), then come here and put it in code blocks:

[code]
# powershell here
[/code]

Or:

```
#powershell here
```

Either one will work. Then I can take a look.

1 Like

First image shown that my newly crafted module (named “example_module” but minus sign appeared involuntarily) is installed in virtual enviroment, among rest of modules:

(lpthw) PS C:\Users\Adam\.venvs\lpthw\projects\example_project> pip list
Package                           Version
--------------------------------- ---------
altgraph                          0.17.2
astroid                           2.4.2
atomicwrites                      1.4.0
attrs                             21.2.0
backports.entry-points-selectable 1.1.0
beautifulsoup4                    4.9.3
blessings                         1.7
bokeh                             2.3.0
certifi                           2020.12.5
chardet                           4.0.0
colorama                          0.4.4
curtsies                          0.3.5
cwcwidth                          0.1.4
distlib                           0.3.3
example-module                    0.1
filelock                          3.3.1
...

And when I invoke pip list outside of virtual enviroment, me example-module is gone.

PS C:\Users\Adam> pip list
Package                           Version
--------------------------------- ---------
astroid                           2.4.2
backports.entry-points-selectable 1.1.0
beautifulsoup4                    4.9.3
blessings                         1.7
bokeh                             2.3.0
certifi                           2020.12.5
chardet                           4.0.0
colorama                          0.4.4
curtsies                          0.3.5
cwcwidth                          0.1.4
distlib                           0.3.3
filelock                          3.3.1

Then, I checked my module with pip show command:

(lpthw) PS C:\Users\Adam\.venvs\lpthw\projects\example_project> pip show example_module
Name: example-module
Version: 0.1
Summary: example project
Home-page: URL to get it at.
Author: Adam
Author-email: stokaratow@gmail.com
License: UNKNOWN
Location: c:\users\adam\.venvs\lpthw\lib\site-packages\example_module-0.1-py3.9.egg
Requires: pytest
Required-by:

And compared it with some other module I have installed earlier with pip:

(lpthw) PS C:\Users\Adam\.venvs\lpthw\projects\example_project> pip show pygame
Name: pygame
Version: 2.0.1
Summary: Python Game Development
Home-page: https://www.pygame.org
Author: A community project.
Author-email: pygame@pygame.org
License: LGPL
Location: c:\users\adam\appdata\local\programs\python\python39\lib\site-packages
Requires:
Required-by:

My conclusion is, I can’t import example_module, because it’s not in main modules directory.
How can I fix this? Should I tinker with sys.path, or have I simply messed up the quiz?

Not really, you said this:

“And when I invoke pip list outside of virtual enviroment, me example-module is gone.”

I think there might be a misunderstanding of what virtualenv does. This is expected behavior because your lpthw virtualenv is like a walled off garden that only has the modules you install while it’s active. In this case, you activated lpthw, then added example-module, and that’s why it’s there.

But, when you exit the lpthw virtualenv then all of the modules you added while inside there disappear. It’s kind of like a mini computer inside your computer. If you had two computers, installed Photoshop on one but not the other, then when you switch computers you’d see Photoshop “disappear” from the one where you never installed it.

So, what you need to do to see the example-module everywhere is install it while not inside any virtualenv. Then it’ll be installed in the global modules location, but I highly recommend you not do this. It’s very hard to manage this and clean it up if you mess it up, but super easy to just erase a virtualenv you borked and start again.

1 Like

Thank you for clarifying this, Zed.

Okay, i uninstalled and removed everything except skeleton and started from scratch.

  1. Copied skeleton/ and named it example_project

  2. Within example_project\example_project created python file named “project_script_a.py”

  3. Within bin/ folder created python script named “top_level_code.py”

  4. Edited “setup.py”, “example_project_tests.py” accordingly to instructions in ex46, pytest shows OK, everything seems to be working.

  5. Ran (lpthw)> python setup.py install

  6. Within example_project\example project created another script named “testing_import.py”. In it, I imported my module from step 5 named “example_project”

  7. Ran (lpthw)> python testing_import.py and it ran without errors.

Did I make this right this time? Did I use venv-contained module correctly?

Seems like it. If you could craft the project, install it, and from within the venv run your thing then that’s it. It does get a lot more complicated with the options but that’s the start.

1 Like

Great, thank you for help!