Python 3 venv: virtual environment tutorial in depth

Поделиться
HTML-код
  • Опубликовано: 26 сен 2024

Комментарии • 32

  • @junsulee
    @junsulee 5 лет назад +2

    Thank you, Eric. I finished watching this. Worth taking 35 minutes time. Really helpful.

    • @ericrosko
      @ericrosko  5 лет назад

      Thanks and good job making it to the end. Now you'll know this stuff for the rest of your career.

  • @bulcub
    @bulcub 5 лет назад +2

    Very excellent tutorial, I enjoyed without being bored! learned quite a bit! Thanks and keep up the good work.

    • @ericrosko
      @ericrosko  5 лет назад

      Thanks, I love it when people make it to the end.

  • @jlpicard7
    @jlpicard7 5 лет назад +1

    Nice job Eric. I ended up watching the whole thing as well even though we work in a windows environment. I found that I could better understand the documentation after watching your video because it clearly went through the major concepts around what virtual environments are and how to work with them. For example, even though you did this on linux/Mac OS, I could figure out that windows users need to run the activate.bat or Activate.bat scripts instead of using the source command. Saved me from much needless confusion. Thanks!

    • @ericrosko
      @ericrosko  5 лет назад

      Well great job yourself and thanks.

  • @ericrosko
    @ericrosko  6 лет назад +6

    Hey, if you are a beginner, this video holds your virtual hand through the creation of a Python virtual environment using the venv command. It's not for the pro user, but its made to get the complete newbie comfortable with the process.

  • @yusufyasin2878
    @yusufyasin2878 5 лет назад +2

    Tutorial is very well made, appreciate the effort you put into this! I was wondering if you could create a continuation to this tutorial telling us how we could take an existing project on the system and turn it into a virtual one, if possible that it.

    • @ericrosko
      @ericrosko  5 лет назад

      Thanks! Sounds like a good video suggestion. However, maybe I can just tell you know. If I had a project just using the local system install of python, I would go into my projects root and pip freeze to create a requirements file. After that I could create a virtual environment (these days I use mkvirtualenv),
      Then to populate your new blank virtual environment with your project's needed dependencies you can start up your virtual environment and pip install the requirements.txt. Pip can now use your requirements.txt file to install the same dependencies into your virtual environment. Now you can delete all your dependencies if you want out of your local environment, it doesn't matter, because you'll have your requirements.txt to re-install them wherever you want.
      So your requirement.txt file is a great way to keep track of your project's dependencies. Don't forget to update it if you install a new package. Thanks again for the comment. I'm so glad people are watching the video and some are even making it all the way through this long tutorial - it shows you have the patience to learn.

  • @derekcollins9739
    @derekcollins9739 4 года назад

    Thank you for this tutorial! You explained everything in detail.
    It helped me a lot. I left a like. 🤗
    The only thing that could have been done better is getting rid of the typing sounds.

  • @josephcottingham7310
    @josephcottingham7310 4 года назад +1

    very helpful, thank you!

  • @meghrajpardesi4248
    @meghrajpardesi4248 6 лет назад +1

    Great Tutorial for beginners. good job keep doing it

    • @ericrosko
      @ericrosko  6 лет назад +1

      Thanks! I'm glad you like it. I was worried no one would have the patience to watch it but there's no quick way to learn this stuff IMO. :-) Stay tuned for my (hopefully soon) video on mkvirtualenvironment.

    • @meghrajpardesi4248
      @meghrajpardesi4248 6 лет назад

      @@ericrosko ya sure, can I talk to you on mail.

  • @mkaack
    @mkaack 5 лет назад +5

    Very good Eric. Didn't think I would watch the whole thing... but did! It was very clear and good demonstrations. I feel very comfortable using virtual environments now.

    • @ericrosko
      @ericrosko  5 лет назад

      Thanks! I'm very happy with this type of comment. :)

  • @wendyslittleprogram3984
    @wendyslittleprogram3984 4 года назад +2

    Thanks for this Eric. I need something like this, but only 3 minutes in and i'm lost. Even though I know I have many packages installed, 'pip list' just returns 'pip, setuptools, wheel'. Any idea why that might be?
    I've been enjoying actually just writing and running python code, but lately I've been trying to understand pip and venv more. It's giving me a massive headache! I seem to have numerous python executable files, mutiple python folders, all over my system, some of them pointing to other locations. I don't know if i've put them there by accident, or if they're in the right place. How do I make sense of it all??

    • @ericrosko
      @ericrosko  4 года назад

      I think this has something to do with which "pip" you are getting when you type pip list. If I type "which pip" (try this in your terminal) you will get the location of that pip command which in my case is /Library/Frameworks/Python.framework/Versions/3.6/bin/pip. Now if I type "pip3 list" I get a location of /Library/Frameworks/Python.framework/Versions/3.7/bin/pip3. My pip list gives me all the packages I installed, and pip3 list for me only gives pip and setup tools. In my case I did most of my work in 3.6 and hardly touched 3.7. "pip2 list" gives me my pip2 location and a different list of packages! This could drive someone mad.
      So what is the solution? I have a suspicion that the word "pip" is just an alias, just like "python" is an alias pointing to the last python version it was symlinked to. You could try to set the link of pip to the version you want. or you could try "pip3" and see if that gives you want you want.
      Sorry, there are no short answers on my channel :)

  • @igorordecha
    @igorordecha 4 года назад +1

    5:19 works on linux too. Thanks

  • @pynchia4119
    @pynchia4119 5 лет назад +1

    Good video for the basics. Why do you use pip3 in the venv created with python3?

    • @ericrosko
      @ericrosko  5 лет назад +1

      I just wanted to make sure I was using the right one. I once had an issue where pip was mapped to a different python version. Right now if I ' cd /Library/Frameworks/Python.framework/Versions/3.6/bin/' I see three different versions of the same file named pip, pip3, and pip3.6. I actually have 3.6 and 3.7 installed on my machine. If I type "pip" it gives me the pip in 3.6/bin and if I type "pip3" it gives me the pip3 in 3.7/bin. Sometimes it can matter. Just like "python" can be hard linked to "python2" and then if you want python 3 you must type python3 unless you change the "python" hard link to point to "python3".

  • @000h-o3w
    @000h-o3w 6 лет назад +1

    Thanks for posting the video and sharing your knowledge. To get the "which" and "source" command working in DOS command prompt, I have to installed something first?

    • @000h-o3w
      @000h-o3w 6 лет назад

      Sorry to mention that I am on windows. I suppose "which" and "source" commands are specific to MacOS. In windows, first create the necessary folders for the designated virtual environment using venv and then firing up the activate.bat within the Scripts directory did the job.

    • @ericrosko
      @ericrosko  6 лет назад +1

      It sounds like you got it figured out. The '.bat' file you found is what I used before to get the same effect as a ".sh" or shell script on the Mac. Definitely check out the windows way of doing things, but the concepts should remain the same.
      I would recommend getting good at PowerShell, because then you get the same (probably with some caveats) as the cmd prompt plus they added a bunch commands straight from Linux like "ls" for list files and the pipe "|" for piping commands. I just tried "which" in PowerShell and it doesn't work, but its still a great thing to know for your file management and searching.

    • @000h-o3w
      @000h-o3w 5 лет назад

      @@ericrosko Thanks Eric for your reply and the nice tip on PowerShell. Will definitely check it out, and the piping command is a very handy feature. Did a quick search on the "which" command and seems like the PowerShell equivalent to "which" is "where", as mentioned here stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-the-windows-command-line. Cheers!

  • @stone8905
    @stone8905 4 года назад

    Thanks for sharing your knowledge it helped me a lot.
    Eric, I was curious, what does "python3 -m" means. -m is like manager or what? can someone give some links that explain that google did not help me

    • @ericrosko
      @ericrosko  4 года назад +1

      Open your terminal and type "python3 --help". This will give you a brief list of various switches. So I see that under -m you can see "run library module as a script". The next step is to paste that text into google, which gives you more info.
      If I remember correctly, it lets you execute some code in the library. Sometimes you just want to import some method/class names from a library, but you can also have code in a file under the if __name__ == '__main__': statement. The code in the if statement only gets execute if the script is execute directly, and if the file is imported it won't run. You can experiment with having the if importbut itwould
      helpful stuff on the terminal by typing "man python3" for manual page. One it opens you ca press spacebar to go down a page, 'b' to go up a page, enter to go down a line, and 'q' to quit. Know you know the magic keyboard keys to get around man pages! Try 'man list', 'man cp', or another command to see the helpful info.
      Good luck.

  • @goranbrannstrom
    @goranbrannstrom Год назад

    As a newbie to python I have to ask: Will I have to source this venv/bin/activate file every single time I want to run one of my python scripts? When I write bash script I can just add a shebang to the first line and I can run it from anywhere without sourcing anything first. Please, tell me there's a simpler way for actually using the scripts after developing them...?

    • @ericrosko
      @ericrosko  Год назад

      While I can't remember the exact part of the video, if I'm typing 'source' in the terminal, it is so I can basically "refresh" the terminal with the environmental variables. This is especially handy if there are a lot of them. When you open a terminal, it loads its startup values from its config file, either .bash_profile or whatever shell config you need. So you can put your repeating commands in the config, then they will run when you open the terminal.
      Basically source is used to reload values into my terminal so I don't have to close and re-open the terminal.
      For large annoying commands sometimes I can use "alias", so I can put the line "alias desk='cd ~/Desktop" in my config and then typing just desk in my terminal will execute that command. There are lots of things like this. If you find it annoying, then someone else has two, and there is a way to make it easier.

  • @goranbrannstrom
    @goranbrannstrom Год назад

    Also, why do you use pip sometimes and pip3 sometimes?

    • @ericrosko
      @ericrosko  Год назад

      pip3 will explicitly call the python3 version of pip. So if you use pip, and installed python2 earlier, then pip might just be calling the python2 version of pip (which I assume is pip2). pip itself might only exist as an alias pointing to the version of pip you want. If I'm setting up a computer and install pip3, but there is no pip, then I must just add pip as an alias so calling pip will call pip3. When in doubt, use that version number after pip (like pip3) and you will never go wrong. It's kind of a gotcha. Thanks for finding my video, its ancient by this point.