What happens when you activate a virtual environment? Remember that if you have multiple versions of Python installed, you may have to do python3.9 -m venv venvName or similar, depending on your Python version. This will create a new virtual environment called venvName in the current folder. Now, this is going to be relevant to virtual environments in a moment, as we'll learn! How to create new virtual environmentsĬreating new virtual environments with recent Python versions is as easy as running this command on your console: python -m venv venvName If it isn't found, Python will raise an ImportError with a message telling you that the thing you tried to import doesn't exist. And then the third, and so forth until it is found. If it doesn't find it, it will move onto the second path of that list. What this means is that, if we try to import something, Python will look for something.py on the first path in the output list. If you run this, you may get something that looks like this: We can see what the import paths are by running this code: import sys When we run a Python file, we have access to import paths, or where Python will look when we try to import things. Similarly, because each virtual environment has its own folder of third-party libraries, they can have different libraries or the same libraries in the same or different versions. That lets us work on projects that use different Python versions very easily.
0 Comments
Leave a Reply. |