cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
634
Views
0
Helpful
0
Comments
npetrele
Cisco Employee
Cisco Employee

Okay, guilty as charged for creating a click bait subject. I love Python. But there are some things about Python that can be annoying if you're just starting out as a Python programmer. Let's tackle a few of them so you don't suffer from the mistakes I have made.

 

Indent Intent

Here's a silly "Hello World" script for you, saved on my PC as hello.py:

 

if __name__ == '__main__':
    print('Hello World')
    print('Hello Again')

 

Suppose you get this error or something like it when you try to run the script:

TabError: inconsistent use of tabs and spaces in indentation

How can that be? This error usually appears when the two indents are different, even if only slightly. The two lines are clearly indented the same, aren't they?  Well, no, not actually. (Technically, they're the same in the above example because that's how the blog software works, but use your imagination.)

Depending on the editor you're using, the two indents can look the same, but be different. One can be a series of spaces, while the other may be a tab. If they differ in this way, Python will complain.

Personally, I set my editors to always use spaces, and show exactly the difference in case there is one. For example, this is how it looks when I use Notepad++:

2022-06-03_135156.png

As you can see, the first indent is spaces, and the second one is a tab. I recommend you set up your editor to show the difference, too. Even better, set up the editor so that the tab key either always produce a tab, or always produces a set number of spaces.

 

Too Pipped To Peep

So you've done the "pip install" for all the libraries you use. Even if you use the same libraries for every app you create, this approach can introduce problems, depending on how you publish your application. Suppose you publish it in the cloud, and the Python libraries in the cloud environment are different versions than the ones you used to develop the app.

Here's a way to assert control over libraries on a project-by-project basis. Use virtual environments.

Create a virtual environment with the following command:

 

python -m venv ./venv

 

This creates a new venv directory below the current director. This next part is personal preference, but I like to create my project directory immediately below the venv directory. So the directory would look something like:

current_directory/venv/myProject

Change to the myProject directory. Now you need to activate the virtual environment so that every library you install only installs into that environment. From the myProject directory, issue this command:

 

..\..\venv\Scripts\activate.bat

 

This is, of course, in Windows, as you can tell by the antiquated back slashes. In any Unix-like environment (Linux, BSD, Mac, etc.), you would do something like this for the whole process, instead:

 

python3 -m venv ./venv
cd venv
mkdir myProject
cd myProject
source ../../venv/bin/activate

 

You should see (venv) in your command prompt.  Now you're working in a virtual environment where everything you "pip install" only gets installed in that particular environment. Just remember to activate the environment every time you work on the project, and you're golden.

 

If you find this information useful, let us know, and we'll continue to post Python tips and common errors -- and believe me, when it comes to common errors, I've made them all.

 

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: