Installing collected packages: urllib3, idna, certifi, chardet, requests Now, install requests with pip : (env) ~/projects/demo-app This is because the requests package is not installed in your virtual environment. Now, if you try re-running the demo.py script, it absolutely won’t work, and you should get the following output: (env) ~/projects/demo-app This is the default behavior when you create a virtual environment. This indicates that there are no packages installed inside your virtual environment. If you run pip freeze again you should now see no packages listed. Prepend means to add something to the beginning of something else, so, in this context, we mean to add (env) to the beginning of your command line prompt. At this point, your terminal (depending on which one you are using) will likely prepend your environment name to your command prompt so that you can tell which environment you are using. To activate the environment, run source env/bin/activate (if you are on Windows the command will be env/Scripts/activate.bat ). If you now list the files in the demo-app directory you will see that you have a created a directory called env: ~/projects/demo-app I am going to use env : ~/projects/demo-app can be any valid path, but convention dictates that we normally use env . To create an environment, use the command python3 -m venv . Instead, as discussed in the previous chapter, we create an environment for each individual project we work on and install packages locally, inside the environment. It is best practice to keep use of Python packages installed globally to an absolute minimum. You can view your global Python packages by running pip freeze .įollow the screencast below if you want to double check that you are up to speed! Run the App in a Virtual Environment You will receive the above output if you do not have requests installed as a global Python package. ModuleNotFoundError: No module named 'requests' If the program does not execute successfully, it is likely that the output will look as follows: ~/projects/demo-app You can view your global Python packages by running pip freeze . The script has executed successfully because you have the requests package installed in your global packages. The 200 status code means that the request to has been made successfully. If the script does execute successfully, the output should look as follows: ~/projects/demo-app You can try visiting it in your browser.Īt this point, you can try to run demo.py as follows: ~/projects/demo-appĭepending on a number of factors, the program may, or may not, execute successfully. The snippet of code above does just that: it’s asking for information - retrieving the website - and it prints the result of the status code: hopefully 200.įor your reference, is a website maintained by the Internet Assigned Numbers Authority so that it can be used in documentation for illustrative purposes. The data you retrieve will come accompanied by many different pieces of information, such as status codes, which are used to describe if your request succeeded, failed, or if the device that responded to you cannot brew coffee because it is, in fact, a teapot! (If you want to learn a bit more, follow our course Understanding the Web.) The request used to retrieve data, such as visiting a website, is called a GET. Remember that when you visit a website in your browser, and generally when you are asking for or sending information on the web, you are making a request. → mkdir projectsĪt this point we create a new directory called demo-app to store code for our project: ~/projectsĬreate a file called demo.py with the following contents: import requests You’re now ready to start writing some code!įirst, create a projects directory to house your project repository. You have received and understood your project brief and you’ve already done some planning of your code architecture. Let’s suppose you are starting a new project at work. What happens when we don't use a virtual environment?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |