0

I'm a beginner, trying to learn by doing. I've learned a bit of Python and Django, and I'm starting a new project (recipe website!). I read that I should be using virtual environments to isolate my projects from one another. Following Corey Schafer's tutorial (https://youtu.be/N5vscPTWKOk) on virtual environments, I created a folder called Environments under my base Code folder (i.e. ~/code/Environments), then created a virtual environment called recipe_project within Environments.

Edit: I am using Ubuntu 18.04, sorry for not specifying that before.

However, I have multiple versions of Python installed, in various folders because I don't know the correct location (is there a correct location?) to install them. Currently I have Python 3.8.3 in my Environments folder, that was the folder where I happened to be when I installed Python 3.8 from the command line. I want to use Python 3.8 to develop my recipe website. Is there a way to specify that? Do I need to specify that?

Clarification: Please note that I am asking about the correct place to install Python, not the place to create my virtual environment(s).

I activated my recipe_project virtual environment, then typed in pip list and got:

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support

Package Version


pip 20.1.1

pkg-resources 0.0.0

setuptools 44.1.1

wheel 0.34.2

I also tried typing in python3 -m pip list because I read that you should be more specific when using pip. When I did that, the deprecation warning disappeared, but the package-version list reverted to the long list that appears if I'm outside my virtual environment.

So:

  1. Is there a way to get my virtual environment to only use Python 3/use Python 3 by default? If I need to install a tool or something, please specify how to install it from the command line (such as do I need to use python3 -m, or another method) and also which folder I should be in when I install it.

  2. In general, which folder should I be in when I install new versions of Python?

Please try to explain it as simply as possible since I am a beginner. I've already spent several hours researching this but at least half of what I read went over my head.

Thanks a lot for your patience and understanding!

1 Answers1

1

You can specify the version of python while creating a virtualenv by using the -p flag. Check this question to find out how many versions of python you have installed in your machine. (See all the answers)

For windows you can simply run which python and it'll display all the versions of python you have installed provided their paths are added to the PATH env variable.

$ virtualenv -p python3.8 venv

If you do a help on virtualenv and find the flag, this is what is shows.

$ virtualenv --help
...
  -p py, --python py            target interpreter for which to create a virtual (either absolute path or identifier string) (default: /usr/bin/python3)
...

As for which folder you should be in.
It doesn't matter.
But, it is recommended that you create the virtualenv inside your project folder just to keep things together because it's just easier than keeping a virtualenv for a project elsewhere.

As long as you activate the virtualenv and then do your work, it doesn't matter where the virtualenv actually is.

Diptangsu Goswami
  • 5,554
  • 3
  • 25
  • 36
  • it sounds like I need to completely remove this virtual environment and recreate it using: ```$ virtualenv -p python3.8 venv``` Would I do this maybe by: ```$ rm -rf recipe-project```? – Joanna Raygoza Jun 09 '20 at 18:30
  • That should work. It takes a few seconds to create a new virtualenv now so there shouldn't be a problem starting fresh. – Diptangsu Goswami Jun 09 '20 at 18:32
  • I still have Python 3.8 installed into my Environments folder as I mentioned earlier: joanna@joanna-X441BA:~/code/Environments$ ls Python-3.8.3 Python-3.8.3.tgz It sounds like you are saying this will not cause any problems and I can scatter my Python installs anywhere in my system? – Joanna Raygoza Jun 09 '20 at 18:37
  • You can create multiple virtualenvs depending on your need. One project usually needs just one virtualenv because it's just a way to separate dependencies and keep them specific to projects. A virtualenv is simply a fresh new installation of python. "Activating" a virtualenv means your terminal will now use that particular installation of python. – Diptangsu Goswami Jun 09 '20 at 19:45
  • Let's say you have 5 bottles of water placed at several places in your house and you can drink water from any of those bottles from anywhere provided you know where the bottle is (path of the bottle). You can make more bottles of water and keep them in any part of your house. Before drinking water from a bottle you have to select a bottle and then drink water from it (activating an virtualenv). Installing something in a virtualenv will be like adding a pinch of colour to the specific bottle that is selected. – Diptangsu Goswami Jun 09 '20 at 19:49
  • Now, ofcourse things would be a lot easier if the bottle you were to use from a room (directory) was in that same room, right? – Diptangsu Goswami Jun 09 '20 at 19:49
  • Hmm, I'm seeing a lot of posts that disagree with that idea. For example https://stackoverflow.com/questions/12184846/where-should-virtualenvs-be-created, which says "It's probably a bad idea to keep the virtualenv directory in the project itself, since you don't want to distribute it (it might be specific to your computer or operating system)." Also the YouTube video I referenced in my original question (https://youtu.be/N5vscPTWKOk) says it's a bad idea to keep them all together. – Joanna Raygoza Jun 09 '20 at 20:06
  • Sorry I can't do chat yet as I am still a new member. I've edited the question to clarify - I am asking about the location for my Python install, not the location where my virtual environments should live. – Joanna Raygoza Jun 09 '20 at 20:09
  • Like I said, it doesn't matter where the virtualenv actually is. virtualenvwrapper is just another tool to create and manage virtualenvs. The tool simply creates virtualenvs in a separate directory which is outside the project. The virtualenv directory is never distributed since a simple requirements.txt file does the job. `.gitignore` files are used to ignore it from `git`'s tracking system so that it does not get added along with the project. – Diptangsu Goswami Jun 09 '20 at 21:25
  • However, since you're not using `virtualenvwrapper` and using `virtualenv` which means you have to manually create a virtualenv at some particular location, it's simply easier to keep the virtualenv in the same directory. – Diptangsu Goswami Jun 09 '20 at 21:25
  • That being said, who am I to stop you if you want to create a virtualenv somewhere far from your project directory? Everytime you activate the environment, you'll have to provide the entire path to that dir and I think that's cumbersome. – Diptangsu Goswami Jun 09 '20 at 21:31
  • As you keep doing more projects and larger projects and projects that you have to maintain for a long period of time, you'll gradually understand the need to just use virtualenv or virtualenvwrapper. It's mostly the coder's personal choice. – Diptangsu Goswami Jun 09 '20 at 21:33