Not sure I'm doing Ex 47 the right way

Im trying to wrap my head around virtualenv and want to make sure I do it right and I have the feeling I’m not.

  1. First I install Virtualenv
  2. Then I create a directory called .venvs
  3. Inside .venvs I create a virtual enviroment called /lphw
  4. I then activate the virtual enviroment
  5. I install Nose inside the virtual enviroment

Then I’m alittle unsure what to do with the skeleton.
Do I create that inside the activated virtual environment as a separate folder called skeleton?
Or does these atm have nothing to do with each other?

Skeleton is the file structure and is separate to installing the nose library into a hidden (hence the dot) virtual environment. Install it at the root of the project not in the .venv folder. This is just for libraries and application version types used on your project.

The skeleton structure is more like a template file structure that’s helpful for what comes next. But it’s worth getting into the habit of creating a venv for every project rather than having all libraries in one.

1 Like

Am I understand it correctly if I my renamed skeleton is my root project - and I install nose in that directory?

If so, I’m not sure where I put my hidden virtual environments? Do I do that in the bin folder?

Sorry I’m watching Zed’s video over an over again and rereading chapter 47 but feel there is a small detail I’m not getting.

hello @ktrager

I am not sure I am doing the correct way.
But I never hide the virtual envionment.

I use to create folder where I want my “project”.
Then cd into it.
Inside it I create the environment with:

python3 -m my_venv venv

Now I have a venv called “my_venv” with the active version of python3 installed on my computer.
To activate this I type:

source my_venv/bin/activate

One can tell there is the name of the venv inside brackets first in the terminal
Looks like this in a linux computer. Something similar in a Mac I guess.

(my_venv) username@linuxpc: ~$

Now I start to install modules I need:
Nose, Pytest (wich I prefer) with:

pip install pytest

When you have activated this venv you can use pytest.
When deactivated one cannot.

Step #5 is what I think is the problem. Can you paste the commands you use (the real one, copy-pasted from your terminal) so we can see?

[code]
# your commands here
[/code]

What do you then do with the skeleton…?

Its a skeleton file structure for a project…

Look at it this way. I went into PyCharm and created a completely new project. I’ve not written any code or anything yet, but look at the image below for what was created from the ‘new project template’. I named the project ‘teststructure’ and that is the project root, or in the book’s case, it would be ‘skeleton’.

20

When you create your virtual environment as detailed above, its just a sub-directory of the project folder (which PyCharm does for me).

The other items in the project template ‘skeleton’ are config files or folders/files that typically you would use in any project. I’ve added a few in mine below for the application files and a few tests.

14

So the virtual environment is part of your project structure. So when you package your project up, it’s all nicely contained in a logical way.

The virtual environment ‘activate’ command you run, really just exports your temporary or project environment path so everything you download to use with it, is also logically contained.

Change the word ‘Skeleton’ to ‘project_root’, or something more logical to you.

1 Like

I do the following:

$ cd python
$ mkdir projects
$ cd projects
$ mkdir skeleton
$ cd skeleton
$ mkdir bin NAME tests docs
$ touch NAME/__init__.py
$ touch tests/__init__.py
$ touch setup.py #I enter the setup.py code from the book.
$ touch tests/NAME_tests.py #I enter the tests/NAME_tests.py from the book.
$ ls
NAME		bin		docs		setup.py	tests
$ nosetests
----------------------------------------------------------------------

Ran 1 test in 0.005s

OK

$ cd ..
$ mkdir .venvs
$ virtualenv --system-site-packages .venvs/lpthw
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/trager/python/projects/.venvs/lpthw/bin/python3.6
Also creating executable in /Users/trager/python/projects/.venvs/lpthw/bin/python
Installing setuptools, pip, wheel...done.

$ source .venvs/lpthw/bin/activate
(lpthw) $ python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

(lpthw) $ quit()
(lpthw) $ pip install nose
Requirement already satisfied: nose in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages

(lpthw) $ ls
skeleton
(lpthw) $ cp -R skeleton newproject 
(lpthw) $ cd newproject
(lpthw) $ mv Name newproject #I update my setup.py
(lpthw) $ cd tests
(lpthw) $ mv NAME_tests.py newproject_tests.py #update code to import newproject
(lpthw) $ cd ..
(lpthw) $ nosetests
----------------------------------------------------------------------

Ran 1 test in 0.005s

OK

Is this how I’m supposed to do it?

I think slightly confused about what the virtual my virtual enviroment is. I thought it was a kind of siloed/contained folder where all my code and project directories would go into, and this folder would kind of run as ‘seperate’ system. But seems the virtual enviroment and the project files does not really have to be together.

Do not cd out of this folder!

Its in here you do:

python3 -m my_venv venv

This creates a folder called the same (my_venv).
command “ls” now should give you:
NAME
bin
docs
setup.py
tests
my_venv

Never put anything in it!
Its for the virtual enviornment only.
Its being filled also when you pip install dependencies (like Nose, Pytest, …)

When you have activated it you install what you need for this project.
its a isolated box that is supposed to prevent from conflicts between different versions of modules.
Perhaps you sometime need an enviornment for python 2.7
Then you need dependecnies suitable for that version.
Putting these in different boxes saves some problems with version conflicts.

1 Like

OK @ulfen69 & @gpkesley I think this makes sense to me now.

So basically the skeleton is the folder I rename and start any new project with.
Inside this new project folder/Renamed skeleton (alongside all the other bits) thats where I place my venv.
And I make the venv hidden for aestetic reasons?

So instead of above I would do something like this:

$ cd python/projects
$ ls
newproject	skeleton
$ cp -R skeleton myapp #change folder and py files according to new project
$ mkdir myapp/.venv
$ mkdir myapp/.venvs/lpthw
$ virtualenv --system-site-packages myapp/.venv/lpthw

Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/trager/python/projects/myapp/.venvs/bin/python3.6
Also creating executable in /Users/trager/python/projects/myapp/.venvs/bin/python
Installing setuptools, pip, wheel…done.

$ source myapp/.venvs/lpthw/bin/activate
$ myapp
$ nosetests
.
----------------------------------------------------------------------
Ran 1 test in 0.006s

OK

Hello @ktrager

I am a little bit curious why you create folder for your virtual envionrment?
And put a dot in front of the name (".venv")
In linux and Mac you would not see this folder with a “ls” command.
One has to use “ls -l” or “ls -la” to see those files/folders.
I dont know if its the same in windows.

Is this a windows command to create the venv?

$ virtualenv --system-site-packages myapp/.venv/lpthw

I have tried a little at work where I have a windows machine but never got any success.

As I understood the use of “skeleton” it was to create once. When a new project is about to start one just copy the whole thing and rename folders to a suitable name.
Then one have a empty structure to start working with.

And please use Pytest instead of Nose.
:slight_smile:

So there are people who do that, putting the .venv inside their project folder, but there’s one huge flaw (or feature depending on how you look at it):

Many search tools will also search this directory.

Most should skip it if you .venv (the . should be ignored), but many don’t. So when you’re looking for just your code, it’ll pull up mountains and mountains of things you don’t want. These also get checked into git on accident all the time, with disasterous consequences if you accidentally release code that proprietary.

That’s why I put them in ~/.venvs instead, under their own directory. Then I just don’t worry about those problems, and it’s the same result really.

Now, if you actually do want to search through all that code the putting it in the project is ideal.

2 Likes

Hello.

I am soo sorry if I misled people with this answer.

I actually thought it was the way to do. Appearently I miss understood this part my self.
As I saw it there was some advantages in doing so:
I knew I had created a venv for this project.
And I could see the name of it. When I started using venvs I sometimes forgot wich name to use to start it

source ???/bin/activate

I wonder if this metphor shows I understood how you use to do:

Company = a project in its folder somewhere in the computer
Truck ( that the company use for deliveries) = a virtual enviornment
car park = ~/.venvs

On monday I work for company A.
In stead of picking up the truck at the company I go to the car park and pick up the truck for company A this day. When finished for the day I park the truck again at the car park.

On Tuesday I work for company B.
… (with truck B)

Is this (undertandable?) a somewhat more correct way to do?

2 Likes

That’s a nice metaphor. :slight_smile:

In your experience what’s the advantage over nose?
I tried to look up the differences on the internet, but seems there is good arguments for and against both of them.

Hello @ktrager

I guess I just find better documentation about Pytest before Nose.
So I just dropped Nose.

But I think that Pytest just use “assert” is the best thing about it (perhaps there is some exceptions).
One does not have to remember which assert to use (assertEqual, …)
And the test output tells you quite well whats the problem.

1 Like

It’s always a matter of choice, but I think Pytest is more pythonic, in terms of readable and utilising existing python keywords; assert.

I’m also a fan of the additional pytest features that make fixtures and running subsets of tests extremely easy and reportable.

Herer is a good comparison if you’ve not seen it: https://blog.daftcode.pl/the-cleaning-hand-of-pytest-28f434f4b684

1 Like

I agree, pytest is way better.

1 Like

It there a handy guide somewhere to install pytest.
I’ve been here: https://docs.pytest.org/en/latest/getting-started.html#

I’ve installed pytest (outside my test enviroment)

In my test i’ve switched from nose to pytest

#from nose.tools import *
import pytest
from ex48 import lexicon

But when I try to test

(lphw)$ pytest 
-bash: pytest: command not found

I also tried

$ pytest --version
-bash: pytest: command not found

Pip seems to have installed pytest, but I can’t get i to work anywhere. What is it I’m not getting?
When I do

$ pip list

then pytest shows, however it does not show in my virtual enviroment. Is this where I go wrong?

It’s best to be in your virtual environment and then pip install pytest as you would any other library.

Maybe uninstall pytest where you installed it globally and then do this.